无法访问API。请求的资源上不存在“Access-Control-Allow-Origin”标头

时间:2017-12-23 01:37:18

标签: javascript html api cors fetch-api

我正在尝试使用获取请求访问API, 在某些网站上,它就像一个魅力,但在其他一些网站上我得到了这个错误, 如果我从浏览器加载链接就可以了......

Failed to load https://bittrex.com/api/v1.1/public/getticker?market=btc-usd:
 No 'Access-Control-Allow-Origin' header is present on the requested resource.
 Origin 'https://mydomaine.com' is therefore not allowed access. If an opaque 
response serves your needs, set the request's mode to 'no-cors' to fetch the  
resource with CORS disabled.

我迷路了,我尝试了一切。 如果我写“no-cors”而不是“cors”

,我会得到完全相同的东西
fetch(urlbittrexbtcusd,{ mode: "cors"})
    .then(function(response) {
          if(response.status == 200) { // Check if response went through
              response.json().then(function(data) { 
                  console.log(data); 
                  bittrexbtcusdprice = parseFloat(data.result.last).toFixed(2);
              });
          } else { // Response wasn't ok. Check dev tools
              console.log("response failed?");
              console.log(response); 
          } 
    });

我已经阅读了这篇文章,但无法弄清楚它试图实现的目标以及如何将我的fetch插入其代码...... https://www.html5rocks.com/en/tutorials/cors/ 在他的例子中,我不明白什么是“方法”。

我在FTP而不在我的服务器上。

1 个答案:

答案 0 :(得分:0)

抓取有点棘手。如果模式不是" no-cors" Fetch()甚至不会尝试发出请求。但如果你设置模式:" no-cors"然后浏览器不会发送所有标题,它只会发送"视为安全"标题,这可能发生了什么。所以问题在于获取和浏览器特性的结合。

Here is the link to fetch specs