简体   繁体   English

cors 请求获取反应

[英]cors request fetch on react

Ok, so odd error.好吧,这么奇怪的错误。 I have a react app that gets data via a fetch function我有一个通过 fetch function 获取数据的反应应用程序

fetch('https://port.abendago.com/index.php?controller=departments&route=getAll')

the app is located on a firebase server so a different domain etc...该应用程序位于 firebase 服务器上,因此不同的域等...

https://vanport-debba.web.app/

Now, in the console, I see the request go out, and the server responds.现在,在控制台中,我看到了请求 go,并且服务器响应了。 If I view the response, I see the data (json data) in the response.如果我查看响应,我会在响应中看到数据(json 数据)。 BUT, the console still gives a CORS error and my app doesn't do anything with the data.但是,控制台仍然给出 CORS 错误,我的应用程序对数据没有做任何事情。

On the server I've added all the various CORS headers (which you can see in the response headers on the above URLS).在服务器上,我添加了所有各种 CORS 标头(您可以在上述 URL 的响应标头中看到)。 My server team says I need to change something on my react server?我的服务器团队说我需要更改我的反应服务器上的某些内容? or the react app?或反应应用程序? I've been trying all sorts of stuff and nothing is working.我一直在尝试各种东西,但没有任何效果。

The webserver (end point server) is running plesk and apache and nginx网络服务器(端点服务器)正在运行 plesk 和 apache 和 nginx

The reason might still be server related.原因可能仍然与服务器有关。 I tried cors requesting to the above url and got response headers as these:我尝试了 cors 向上述 url 请求并得到如下响应头:

Access-Control-Allow-Origin: '*'
Access-Control-Allow-Origin: 'https://vanport-debba.web.app/'
Access-Control-Allow-Origin: 'https://vanport-debba.web.app'
Access-Control-Allow-Origin: https://vanport-debba.web.app/
Access-Control-Allow-Origin: https://vanport-debba.web.app
Access-Control-Allow-Origin: *

As you can see, it's sending multiple response headers for Access-Control-Allow-Origin which is precisely what the cors error that I got had:如您所见,它正在为Access-Control-Allow-Origin发送多个响应标头,这正是我遇到的 cors 错误:

Access to fetch at ' http://port.abendago.com/index.php?controller=departments&route=getAll ' from origin ' http://localhost:3000 ' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values ' http://localhost:3000 , '*', ' https://vanport-debba.web.app/ ', ' https://vanport-debba.web.app ', https://vanport-debba.web.app/ , https://vanport-debba.web.app , *', but only one is allowed. Access to fetch at ' http://port.abendago.com/index.php?controller=departments&route=getAll ' from origin ' http://localhost:3000 ' has been blocked by CORS policy: The 'Access-Control-Allow -Origin' header contains multiple values ' http://localhost:3000 , '*', ' https://vanport-debba.web.app/ ', ' https://vanport-debba.web.app ', https ://vanport-debba.web.app/ , https://vanport-debba.web.app , *', 但只允许一个。 Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.让服务器发送具有有效值的 header,或者,如果不透明的响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。

ie IE

The 'Access-Control-Allow-Origin' header contains multiple values 'Access-Control-Allow-Origin' header 包含多个值

Have the server send the header with a valid value让服务器发送具有有效值的 header

If you look at the Mozilla developer docs , its mentioned如果您查看Mozilla 开发人员文档,它会提到

More than one Access-Control-Allow-Origin header was sent by the server.服务器发送了多个 Access-Control-Allow-Origin header。 This isn't allowed.这是不允许的。

Have your server team revisit this and take a look at the cors configuration.让您的服务器团队重新访问并查看 cors 配置。 It should simply return Access-Control-Allow-Origin: *它应该简单地返回Access-Control-Allow-Origin: *

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM