简体   繁体   English

XMLHttprequest无法使用twitch.tv API

[英]XMLHttprequest not working the twitch.tv api

So I've been trying to access the twitch.tv api but every time i go to make a request i keep on getting the error: 所以我一直在尝试访问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."

I'm currently using the live-server package during the development process other wise I'm just using html, css, javascript. 我目前在开发过程中使用的是live-server软件包,否则我只是使用html,css,javascript。 Here is my 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);
   }
}

Does anyone have any ideas as to why am I encountering this error? 是否有人对我为什么会遇到此错误有任何想法?

The Twitch.tv API doesn't support CORS. Twitch.tv API不支持CORS。 You need to use JSON-P (aka JSONP) to work around it. 您需要使用JSON-P(又名JSONP)来解决它。 Read more about that in the Twitch.tv API docs . Twitch.tv API文档中了解有关此内容的更多信息。 There is a prior answer on how to make a JSONP request with native JavaScript that may be useful. 关于如何使用本机JavaScript发出JSONP请求可能有一个预先的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM