你很接近,你链接到的帖子基本上描述了使用跨域请求进行页面抓取YQL(您可以查看源代码以了解到底发生了什么)。您可以删除该插件并使用 jQuery 通过常规 JSONP 请求完成相同的操作:
function getCrossDomainJson(url, callback) {
$.ajax({
url: "http://query.yahooapis.com/v1/public/yql?callback=?",
data: {
q: 'select * from xml where url="' + url + '"',
format: "json"
},
dataType: "jsonp",
success: callback
});
}
基本上这个函数的作用是调用 Yahoo 的查询 api 并运行一个查询。当响应返回时,返回的脚本将调用 jQuery 提供的回调函数(这就是 JSONP 成为可能的原因)。
The 查询您正在使用的(指定在q
参数)是针对 XML 提要的,因此您需要使用select * from xml
检索数据。然后,您可以告诉 Yahoo 以 JSON 格式提供结果(我建议使用此格式而不是 XML;XML 是命名空间的)。
现在,当您调用此函数时:
getCrossDomainJson("http://xproshowcasex.channel-api.livestream-api.com/2.0/getstream", function(data) {
// data is in JSON format:
// make sure you can access the isLive property
if (data && data.query && data.query.results && data.query.results.channel) {
alert(data.query.results.channel.isLive);
}
});
回调函数接收通过YQL检索到的JSON数据并找到isLive
财产。
Example: http://jsfiddle.net/andrewwhitaker/YAGvd/