简体   繁体   English

Javascript 获取 CORS header 缺少“访问控制允许来源”

[英]Javascript Fetch CORS header ‘Access-Control-Allow-Origin’ missing

When running the following fetch request in my React project:在我的 React 项目中运行以下获取请求时:

const options = { method: "GET", headers: { accept: "application/json", "Access-Control-Allow-Origin": "*" } };
      response = await fetch(
        `https://api.b365api.com/v1/bet365/upcoming?league_id=${league}&sport_id=${sport.id}&token=TOKEN`,
        options
      ).catch((err) => console.error(err));

The request returns with the error Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.b365api.com/v1/bet365/upcoming?league_id=131095496&sport_id=1&token=TOKEN. (Reason: CORS header 'Access-Control-Allow-Origin' missing). Status code: 200.请求返回错误Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.b365api.com/v1/bet365/upcoming?league_id=131095496&sport_id=1&token=TOKEN. (Reason: CORS header 'Access-Control-Allow-Origin' missing). Status code: 200. Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.b365api.com/v1/bet365/upcoming?league_id=131095496&sport_id=1&token=TOKEN. (Reason: CORS header 'Access-Control-Allow-Origin' missing). Status code: 200.

I've searched through A LOT of different pages and haven't found an answer I can use other than using a proxy of some sort like https://cors-anywhere.herokuapp.com/ which has a limit and just doesn't work in production.我搜索了很多不同的页面,但没有找到我可以使用的答案,除了使用某种代理,比如https://cors-anywhere.herokuapp.com/有限制而且没有限制在生产中工作。

The error message you are encountering is due to a restriction in the browser called the same-origin policy , which only allows web pages to make requests to resources hosted on the same domain.您遇到的错误消息是由于浏览器中称为同源策略的限制,它只允许 web 页面向同一域上托管的资源发出请求。 To bypass this restriction, the server that the resource is hosted on needs to include an Access-Control-Allow-Origin (it need be in the server, not in your client) header in its response, indicating which domains are allowed to make requests.要绕过此限制,托管资源的服务器需要在其响应中包含一个Access-Control-Allow-Origin (它需要在服务器中,而不是在您的客户端中)header,指示允许哪些域发出请求.

Since the server is not including this header, you have two options:由于服务器不包括这个 header,您有两个选择:

Use a proxy service : As you mentioned, you can use a proxy service such as cors-anywhere.herokuapp.com to make the request, but this has limitations and is not suitable for production.使用代理服务:正如您提到的,您可以使用诸如 cors-anywhere.herokuapp.com 之类的代理服务来发出请求,但这有局限性并且不适合生产。

Create and use your proxy service : Creating a backend for frontend (BFF) is another option.创建和使用代理服务:为前端创建后端 (BFF) 是另一种选择。 Your BFF can act as a proxy and make the request to the https://api.b365api.com on behalf of your frontend.您的 BFF 可以充当代理并代表您的前端向https://api.b365api.com发出请求。 The BFF can then add the necessary Access-Control-Allow-Origin header to the response before forwarding it to the frontend. BFF 然后可以在将响应转发到前端之前将必要的 Access-Control-Allow-Origin header 添加到响应中。 This way, you won't have to worry about the same-origin policy and can make requests to the API without any restrictions.这样,您就不必担心同源策略,并且可以不受任何限制地向 API 发出请求。 You can implement this in any language and framework of your choice, as long as it can make HTTP requests and add headers to the responses.您可以使用您选择的任何语言和框架来实现它,只要它可以发出 HTTP 请求并向响应添加标头即可。 Once your BFF is set up, you can simply make requests to your BFF from your frontend instead of directly to https://api.b365api.com .一旦你的 BFF 设置好,你可以简单地从你的前端向你的 BFF 发出请求,而不是直接向https://api.b365api.com

Ask the server owner to add the header : Contact the owner of the server and ask them to include the Access-Control-Allow-Origin header in their responses with a value of * to allow requests from any domain, or with the domain of your website to allow requests only from that domain.要求服务器所有者添加 header :联系服务器所有者并要求他们在他们的响应中包含 Access-Control-Allow-Origin header,其值为 * 以允许来自任何域的请求,或使用您的域网站只允许来自该域的请求。

Does this or this helps you? 这个这个对你有帮助吗?

In my experience, there are somethings that can cause CORS errors: For me, the most common issue is a backend function error... this happens a lot when I'm coding a new feature and happens an unhandled exception in my code.根据我的经验,有些事情可能会导致 CORS 错误:对我来说,最常见的问题是后端 function 错误......当我编写新功能时经常会发生这种情况,并且在我的代码中发生未处理的异常。 This throws CORS error.这会引发 CORS 错误。

Or maybe you have to config your backend server to use CORS:或者您可能必须将后端服务器配置为使用 CORS:

import express from 'express'
import cors from 'cors'

const app = express()
app.use( cors() ) // - here

Hope I had helped you, good luck希望我帮到你了,祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript:缺少 CORS 标头“Access-Control-Allow-Origin” - JavaScript: CORS header 'Access-Control-Allow-Origin' missing CORS标头“ Access-Control-Allow-Origin”丢失,但它出现在标头中 - CORS header 'Access-Control-Allow-Origin' missing but it present in the header 缺少CORS标头'Access-Control-Allow-Origin' - The CORS Header 'Access-Control-Allow-Origin' is missing (原因:CORS标头“ Access-Control-Allow-Origin”缺失) - (Reason: CORS header ‘Access-Control-Allow-Origin’ missing) 标题CORS“Access-Control-Allow-Origin”缺少Codeigniter - header CORS “Access-Control-Allow-Origin” missing Codeigniter Apache:缺少CORS标题“Access-Control-Allow-Origin” - Apache : CORS header 'Access-Control-Allow-Origin' missing CORS标题'Access-Control-Allow-Origin'缺少Cordova - CORS header 'Access-Control-Allow-Origin' missing Cordova CORS标头'缺少Access-Control-Allow-Origin - CORS header 'Access-Control-Allow-Origin missing CORS header 'Access-Control-Allow-Origin' 缺失错误 - CORS header ‘Access-Control-Allow-Origin’ missing error django & javascript fetch():CORS 政策:没有“访问控制允许来源”Z099FB995346F31C79E3ZF6 存在 - django & javascript fetch(): CORS policy: No 'Access-Control-Allow-Origin' header is present
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM