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

前端 未结 1 1579
长情又很酷
长情又很酷 2021-01-15 00:38

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 reques

1条回答
  •  囚心锁ツ
    2021-01-15 01:06

    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 讨论(0)
提交回复
热议问题