开发者

Can someone help me with using livestream's api to make a cross domain xml request?

开发者 https://www.devze.com 2023-03-26 23:25 出处:网络
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

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 reque开发者_开发知识库sts. 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.


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消