因此,我一直试图访问twitch.tv api,但是每次我提出请求时,我都会继续获取错误:
"XMLHttpRequest cannot load https://api.twitch.tv/kraken/streams/normalice. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8080' is therefore not allowed access."目前,我在开发过程中使用的是实时服务器包,其他明智的方法是使用html、css、javascript。这是我的javascript:
var req = new XMLHttpRequest();
var url = 'https://api.twitch.tv/kraken/streams/normalice';
req.open("GET", url, true);
req.send();
req.onreadystatechange = function () {
if (req.status == 200 && req.readState == 4){
// do stuff here
console.log('hurray it worked');
}else{
console.log(req.statusText);
console.log(req.responseText);
}
}有人知道我为什么会遇到这个错误吗?
发布于 2015-06-11 06:13:36
Twitch.tv API不支持CORS。您需要使用JSONP(又名JSONP)来绕过它。在Twitch.tv API文档中阅读更多有关这方面的内容。有一个可能有用的关于如何使用本机JavaScript发出JSONP请求的事先回答。
https://stackoverflow.com/questions/30772644
复制相似问题