【问题标题】:XMLHttprequest not working the twitch.tv apiXMLHttprequest 无法使用 twitch.tv api
【发布时间】: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


    【解决方案1】:

    Twitch.tv API 不支持 CORS。您需要使用 JSON-P(又名 JSONP)来解决它。在the Twitch.tv API docs 中阅读更多相关信息。有一个prior answer on how to make a JSONP request with native JavaScript 可能有用。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-11
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 2011-02-28
    相关资源
    最近更新 更多