0

Livestream APIを使用して、特定のチャネルがライブであるかどうかを確認しようとしていますが、次のエラーが発生し続けます。

XMLHttpRequest cannot load http://channel.api.livestream.com/1.0/livestatus?channel=huskystarcraft. Origin http://www.webdevstl.com is not allowed by Access-Control-Allow-Origin.

PHPを介して実行する必要がありますか、それともajax呼び出しで何か問題がありますか?これは非常に単純なコードです。

function getActive(){
    if(window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {   
            var json = JSON.parse(xmlhttp.responseText);
            console.log(json);
        }
    }

    xmlhttp.open("GET", "http://channel.api.livestream.com/1.0/livestatus?channel=huskystarcraft", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send();
}
getActive();
4

1 に答える 1

2

You're running into restrictions imposed by the Same Origin Policy. In short, AJAX calls to a different domain are prohibited and will fail - unless explicitly permitted by the remote host.

You need to either use JSONP (mostly applicable to data returned by APIs) or proxy the request through your own server/domain.

CORS would also be an option, but that assumes you having access to the remote server's config.

于 2012-08-25T18:46:20.417 に答える