2

First of all, I was using this (maybe old) code in Javascript:

function GetJson(url)
{
    // 1. New Object XMLHttpRequest
    var xhr = new XMLHttpRequest();

    // 2. Config it 
    xhr.open('GET', url, false);

    // 3. Send request
    xhr.send();

    // 4. Errors 
    if (xhr.status != 200) {
        // Responce error out
        return( xhr.status + ': ' + xhr.statusText ); // 404: Not Found
    } else {
        // Responce result
        return( xhr.responseText ); // responseText -- 
    }
}

This code solved the problem. Until this URL came:

https://bittrex.com/api/v1.1/public/getmarketsummaries/

The first error I encountered is:

XMLHttpRequest can not load https://bittrex.com/api/v1.1/public/getmarketsummaries/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'file: //' is therefore not allowed access.

To not change anything in the browser, long searches have led me to the fetch() function. But I categorically cannot understand how to get a response from the server in the form of JSON or at least in the form of text (for further conversion to JSON)

I try this:

fetch('https://bittrex.com/api/v1.1/public/getmarketsummaries/',{mode:'no-cors'})  
  .then(  
    function(response) {  
      if (response.status !== 200) {  
        console.log('Looks like there was a problem. Status Code: ' +  
          response.status);  
        return;  
      }

      // Examine the text in the response  
      response.json().then(function(data) {  
        console.log(data);  
      });  
    }  
  )  
  .catch(function(err) {  
    console.log('Fetch Error :-S', err);  
  });

And I get the answer:

Looks like there was a problem. Status Code: 0

Is there any way to get the data? It is desirable in a variable. Because I just do not understand how fetch works.

I just need to get data from the API for further processing.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Ievgen
  • 205
  • 4
  • 11
  • 1
    Try to change the mode to mode: 'cors' as JavaScript may not access any properties of the resulting response in case the mode is 'no-cors' – Hussain Mahfoodh Aug 25 '17 at 11:25
  • if "cors" , then **No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'file://' 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.** – Ievgen Aug 25 '17 at 11:35
  • 1
    Your call will work if you remove `mode:'no-cors'` from it and prefix the request URL in your fetch call with `https://cors-anywhere.herokuapp.com/`, like this: `fetch('https://cors-anywhere.herokuapp.com/' + 'https://bittrex.com/api/v1.1/public/getmarketsummaries/')`. For an explanation of why that works, see the **How to use a CORS proxy to get around “No Access-Control-Allow-Origin header” problems** section of the answer at https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe/43881141#43881141 – sideshowbarker Sep 06 '17 at 19:58

2 Answers2

3

You still have problem with cors. Mode "no-cors" just does not work as you expecting.

  • The only solution is to disable security in the browser? There are no other solutions? – Ievgen Aug 25 '17 at 11:31
  • It is not a security feature which can be disabled. The only solution would be to contact the admin of the domain and request them to allow cross-origin access to the URL you want to load data from. – Thijs Aug 25 '17 at 12:29
3

What you're dealing with is Cross-Origin Resource Sharing (CORS). The URL you're requesting the data from doesn't allow the data to be fetched from another domain. The reason your second snippet works is because you set the mode to no-cors. The call will succeed but your code won't be able to access any of the data. It is kinda useless for your purpose.

Thijs
  • 2,341
  • 2
  • 14
  • 22