0

I'm trying to access an API with a fetch request, On some website it works like a charm, but on some others I get this error, If I load the link from my browser it works...

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.

I'm lost, and I tried everything. I get the exact same thing if I write "no-cors" instead of "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); 
          } 
    });

I've read this article, but can't figure out what it tries to achieve and how to plug my fetch to its code... https://www.html5rocks.com/en/tutorials/cors/ I don't understand what is "method" referring to, in his example.

I'm on an FTP and not on my server.

Ilan
  • 479
  • 7
  • 17
  • 1
    Being new is not a good enough excuse to NOT do your own research of the problem before asking. There are plenty of resources out there. Have you tried putting the title of your question into Google? – marekful Dec 23 '17 at 01:48
  • I found the question that was flagged as duplicate, however the fact that it does it only for some website and not others changes things. – Ilan Dec 23 '17 at 01:49
  • Also I cannot change my server settings I'm on a FTP – Ilan Dec 23 '17 at 01:52

1 Answers1

0

Fetch is a bit tricky. Fetch() won't even try making a request if mode is not "no-cors" but if you set mode: "no-cors" then the browser won't send all headers, it will only send "deemed safe" headers, that's what probably happening. So the problem is the combination of both fetch and browser peculiarities.

Here is the link to fetch specs

dan tenovski
  • 311
  • 2
  • 10
  • I don't understand then, I'm in a sort of infinite loop of not being able to do anything.. Why does it work with certain website API and not others then... – Ilan Dec 23 '17 at 12:47
  • Why am I able to fetch with this code on other API if it won't try to fetch if not no cors? – Ilan Dec 25 '17 at 00:42