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

亡梦爱人 提交于 2019-12-01 09:43:01
Andrew Whitaker

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/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!