有人可以帮我使用 livestream 的 api 发出跨域 xml 请求吗?

发布于 2024-11-28 18:18:22 字数 988 浏览 1 评论 0原文

我正在尝试使用 livestream 非常有用的移动 api,位于 http://www.livestream .com/userguide/?title=Mobile_API#Requesting_a_mobile_stream 发出 xml 请求。我感兴趣的是 isLive 响应值。我正在尝试使用像这样的ajax请求

$.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
   }

我正在使用 http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/ 发出跨域 xml 请求。谁能告诉我这是否是实现这一目标的最有效方法?我还没能让它发挥作用。当我运行 console.log(xml) (这可能不正确)时,JS 控制台显示 objectObject,我认为这意味着我需要解析数据?如果有人能花时间解释这一点,我会很高兴。非常感谢。

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
   }

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薆情海 2024-12-05 18:18:22

您很接近,您链接到的帖子基本上描述了使用通过 YQL (您可以查看源代码以了解到底发生了什么)。您可以删除该插件,并使用 jQuery 通过常规 J​​SONP 请求完成相同的操作:

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 成为可能的原因)。

您使用的查询(在 q 参数中指定)是针对 XML Feed 的,因此您需要使用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 属性。

示例: http://jsfiddle.net/andrewwhitaker/YAGvd/

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
    });
}

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).

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).

Now, when you call this function:

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);
    }
});

The callback function receives the JSON data retrieved via YQL and finds the isLive property.

Example: http://jsfiddle.net/andrewwhitaker/YAGvd/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文