【发布时间】:2015-08-26 16:01:32
【问题描述】:
所以我一直在尝试访问 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."
我目前在开发过程中使用 live-server 包,否则我只使用 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);
}
}
有没有人知道我为什么会遇到这个错误?
【问题讨论】:
标签: javascript api cors xmlhttprequest twitch