1. <small id='pmHnd'></small><noframes id='pmHnd'>

      <legend id='pmHnd'><style id='pmHnd'><dir id='pmHnd'><q id='pmHnd'></q></dir></style></legend>

    1. <i id='pmHnd'><tr id='pmHnd'><dt id='pmHnd'><q id='pmHnd'><span id='pmHnd'><b id='pmHnd'><form id='pmHnd'><ins id='pmHnd'></ins><ul id='pmHnd'></ul><sub id='pmHnd'></sub></form><legend id='pmHnd'></legend><bdo id='pmHnd'><pre id='pmHnd'><center id='pmHnd'></center></pre></bdo></b><th id='pmHnd'></th></span></q></dt></tr></i><div id='pmHnd'><tfoot id='pmHnd'></tfoot><dl id='pmHnd'><fieldset id='pmHnd'></fieldset></dl></div>
        <bdo id='pmHnd'></bdo><ul id='pmHnd'></ul>

      <tfoot id='pmHnd'></tfoot>

      无法使用 Fetch API 从 localhost 加载 Deezer API 资源

      时间:2023-09-03
      1. <tfoot id='IAJpK'></tfoot>
        <i id='IAJpK'><tr id='IAJpK'><dt id='IAJpK'><q id='IAJpK'><span id='IAJpK'><b id='IAJpK'><form id='IAJpK'><ins id='IAJpK'></ins><ul id='IAJpK'></ul><sub id='IAJpK'></sub></form><legend id='IAJpK'></legend><bdo id='IAJpK'><pre id='IAJpK'><center id='IAJpK'></center></pre></bdo></b><th id='IAJpK'></th></span></q></dt></tr></i><div id='IAJpK'><tfoot id='IAJpK'></tfoot><dl id='IAJpK'><fieldset id='IAJpK'></fieldset></dl></div>
      2. <legend id='IAJpK'><style id='IAJpK'><dir id='IAJpK'><q id='IAJpK'></q></dir></style></legend>
          <tbody id='IAJpK'></tbody>
          <bdo id='IAJpK'></bdo><ul id='IAJpK'></ul>

                <small id='IAJpK'></small><noframes id='IAJpK'>

                本文介绍了无法使用 Fetch API 从 localhost 加载 Deezer API 资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试从 localhost 访问 Deezer API,但我不断收到以下错误:

                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/autocomplete?limit=1&q=eminem.
                Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
                Origin 'http://localhost' 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.
                

                localhost 的响应标头确实有 Access-Control-Allow-Origin 标头(访问控制允许来源:*).

                localhost's response's headers does have Access-Control-Allow-Origin header (Access-Control-Allow-Origin:*).

                我正在使用 fetch,例如:fetch('http://api.deezer.com/search/track/autocomplete?limit=1&q=eminem').

                I'm using fetch like: fetch('http://api.deezer.com/search/track/autocomplete?limit=1&q=eminem').

                我做错了什么?

                推荐答案

                您可以通过公共CORS代理发出请求;为此,请尝试将您的代码更改为:

                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')
                

                通过 https://cors-anywhere.herokuapp.com 发送请求,该请求转发请求 http://api.deezer.com/search/track/autocomplete?limit=1&q=eminem 然后收到响应.https://cors-anywhere.herokuapp.com 后端将 Access-Control-Allow-Origin 标头添加到响应中,并将其传递回您的请求前端代码.

                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.

                然后浏览器将允许您的前端代码访问响应,因为带有 Access-Control-Allow-Origin 响应标头的响应是浏览器看到的.

                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.

                您还可以使用 https://github.com 轻松设置自己的 CORS 代理/Rob--W/cors-anywhere/

                详细了解当您使用 XHR 或 JavaScript 库中的 Fetch API 或 AJAX 方法从前端 JavaScript 代码发送跨域请求时浏览器会做什么,以及有关必须接收哪些响应标头才能让浏览器允许前端代码的详细信息访问响应 - 请参阅 https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS.

                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.

                这篇关于无法使用 Fetch API 从 localhost 加载 Deezer API 资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Google 的 Places API 和 JQuery 请求 - Access-Control-Allo 下一篇:XMLHttpRequest:网络错误 0x80070005,在 Microsoft Edge(但

                相关文章

                最新文章

                  <i id='QYKIw'><tr id='QYKIw'><dt id='QYKIw'><q id='QYKIw'><span id='QYKIw'><b id='QYKIw'><form id='QYKIw'><ins id='QYKIw'></ins><ul id='QYKIw'></ul><sub id='QYKIw'></sub></form><legend id='QYKIw'></legend><bdo id='QYKIw'><pre id='QYKIw'><center id='QYKIw'></center></pre></bdo></b><th id='QYKIw'></th></span></q></dt></tr></i><div id='QYKIw'><tfoot id='QYKIw'></tfoot><dl id='QYKIw'><fieldset id='QYKIw'></fieldset></dl></div>
                  <tfoot id='QYKIw'></tfoot>

                  1. <legend id='QYKIw'><style id='QYKIw'><dir id='QYKIw'><q id='QYKIw'></q></dir></style></legend>

                    <small id='QYKIw'></small><noframes id='QYKIw'>

                      <bdo id='QYKIw'></bdo><ul id='QYKIw'></ul>