Cannot load Deezer API resources from localhost with the Fetch API

前端 未结 3 1875
灰色年华
灰色年华 2020-12-03 15:57

I\'m trying to access the Deezer API from localhost, but I\'m keep getting the following error:

Fetch API cannot load http://api.deezer.com/search/track/auto         


        
相关标签:
3条回答
  • 2020-12-03 16:05

    For now, it is impossible to make this request. You can proxy request to API from your own backend or use jsonp. Here is a library with fetch-like syntax https://github.com/camsong/fetch-jsonp. Usage example https://jsfiddle.net/4dmfo0dd/1/

    fetchJsonp('https://api.deezer.com/search/track/autocomplete?limit=1&q=eminem&output=jsonp')
    .then(function(response) {
        return response.json();
      })
      .then(json => console.log(json))
      .catch(function(error) { console.log(error); });
    
    0 讨论(0)
  • 2020-12-03 16:13

    You can make the request through a public CORS proxy; to do that try changing your code to:

    fetch('https://cors-anywhere.herokuapp.com/http://api.deezer.com/search/track/autocomplete?limit=1&q=eminem')
    

    That sends the request through https://cors-anywhere.herokuapp.com, which forwards the request to http://api.deezer.com/search/track/autocomplete?limit=1&q=eminem and then receives the response. The https://cors-anywhere.herokuapp.com backend adds the Access-Control-Allow-Origin header to the response and passes that back to your requesting frontend code.

    The browser will then allow your frontend code to access the response, because that response with the Access-Control-Allow-Origin response header is what the browser sees.

    You can also easily set up your own CORS proxy using https://github.com/Rob--W/cors-anywhere/

    For details about what browsers do when you send cross-origin requests from frontend JavaScript code using XHR or the Fetch API or AJAX methods from JavaScript libraries—and details about what response headers must be received in order for browsers to allow frontend code to access the responses—see https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS.

    0 讨论(0)
  • 2020-12-03 16:13

    CORS or Cross Origin requests made to servers

    http://api.deezer.com/search/track/autocomplete?limit=1&q=eminem

    in this case, have a preflight check enabled by all modern browsers.

    and usually fail, if the server does not respond with Access-control headers.

    In case of a fetch too, since you are basically fiddling with Javascript ,

    You Need the Server to respond with Access-control-Allow-Origin Headers, which are flexible.

    You Can not do Much about it Unless, the API itself becomes flexible and more open.

    You however can Use fetch with mode set to no-cors

    IFFF you only wish to cache the result of the request you make, to serve as a response, and not consume it yourself

    Read Opaque Responses

    No-CORS Definition

    no-cors — Prevents the method from being anything other than HEAD, GET or POST. If any ServiceWorkers intercept these requests, they may not add or override any headers except for these. In addition, JavaScript may not access any properties of the resulting Response. This ensures that ServiceWorkers do not affect the semantics of the Web and prevents security and privacy issues arising from leaking data across domains

    0 讨论(0)
提交回复
热议问题