有人可以帮助我使用livestream的api做一个跨域xml请求吗? [英] Can someone help me with using livestream's api to make a cross domain xml request?
问题描述
我想使用livestream的非常有用的移动API,位于 http:// www.livestream.com/userguide/?title=Mobile_API#Requesting_a_mobile_stream 发出xml请求。我感兴趣的是isLive响应值。我尝试使用像这样的ajax请求
I'm trying to use livestream's extremely helpful mobile api found at http://www.livestream.com/userguide/?title=Mobile_API#Requesting_a_mobile_stream to make an xml request. All I am interested in is the isLive response value. I am trying to use an ajax request like this
$.ajax({
type: "GET",
url: "http://xproshowcasex.channel-api.livestream-api.com/2.0/getstream",
datatype: "xml",
success: function(xml){
//this is where I need help. This is what I would like to happen
if (isLive == true) {
//perform action
}
else {
//perform other action
}
$ b b
我使用的插件位于 http:// james .padolsey.com / javascript / cross-domain-requests-with-jquery / 做跨域xml请求。任何人都可以告诉我,这是否是最有效的方法来完成这个?我无法当我运行console.log(xml)(这可能是不对的)JS控制台显示objectObject,我认为意味着我需要解析数据?我会喜欢它,如果有人可以花时间解释这个。非常感谢。
I am using the plugin found at http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/ to make cross domain xml requests. Can anyone tell me if this is the most effective way to accomplish this? I haven't been able to get it to work. When I run the console.log(xml) (which probably isn't right) the JS console shows objectObject, which I think means I need to parse the data? I would love it if someone could take the time to explain this. Thanks so much.
推荐答案
关闭后,您链接的帖子主要描述了使用跨域请求的页面抓取通过 YQL (您可以查看源代码来准确了解发生了什么)。你可以裁剪插件,并使用jQuery的常规JSONP请求完成相同的事情:
You are close, the post you linked to basically describes page-scraping using a cross-domain request that goes through YQL (You can peek at the source to see exactly what's going on). You can cut out the plugin and accomplish the same thing with a regular JSONP request with jQuery:
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
});
}
这个函数基本上是调用雅虎的查询api与一个查询运行。当响应返回时,脚本返回调用jQuery提供的回调函数(这就是使JSONP成为可能)。
Basically what this function does is call Yahoo's query api with a query to run. When the response comes back, the script returned calls a callback function that jQuery supplies (this is what makes JSONP possible).
查询您使用(在 q中指定
参数)是针对XML Feed,因此您需要使用 select * from xml
检索数据。然后你可以告诉雅虎给你JSON格式的结果(我建议使用这个而不是XML; XML是命名空间)。
The query you're using (specified in the q
parameter) is against an XML feed, so you need to use select * from xml
to retrieve the data. You can then tell Yahoo to give you the result in JSON format (I would recommend using this instead of XML; the XML was namespaced).
现在,当你调用函数:
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
属性。
The callback function receives the JSON data retrieved via YQL and finds the isLive
property.
示例: http://jsfiddle.net/andrewwhitaker/YAGvd/
这篇关于有人可以帮助我使用livestream的api做一个跨域xml请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!