XMLHttprequest 不工作 twitch.tv api
XMLHttprequest not working the twitch.tv api
所以我一直在尝试访问 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);
}
}
有人知道我为什么会遇到此错误吗?
Twitch.tv API 不支持 CORS。您需要使用 JSON-P(又名 JSONP)来解决它。在 the Twitch.tv API docs. There is a prior answer on how to make a JSONP request with native JavaScript 中阅读更多可能有用的信息。
所以我一直在尝试访问 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);
}
}
有人知道我为什么会遇到此错误吗?
Twitch.tv API 不支持 CORS。您需要使用 JSON-P(又名 JSONP)来解决它。在 the Twitch.tv API docs. There is a prior answer on how to make a JSONP request with native JavaScript 中阅读更多可能有用的信息。