【发布时间】:2018-08-27 06:34:48
【问题描述】:
我正在使用 react 前端和 nodeJs v6 后端,以及 socket.io-redis pub sub 方法。在这种情况下,浏览器会拒绝连接。
我有这个错误:
Failed to load https://api.mydomain.com/socket.io-client/?EIO=3&transport=polling&t=M8vzD5C: The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:3000, *', but only one is allowed. Origin 'http://localhost:3000' is therefore not allowed access.
我在前端反应,后端 nodeJs 上运行,带有 pub sub socket-redis 连接
这是我的响应标题:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, If-Modified-Since, Authorization
Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin: http://localhost:3000
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 104
Content-Type: text/plain; charset=UTF-8
Date: Sun, 18 Mar 2018 16:19:12 GMT
Server: nginx/1.10.3 (Ubuntu)
我可以知道我做错了什么吗
显然可以建立连接
但它立即中断并继续尝试重新连接循环(我知道为什么)
但是为什么我的响应标头会有多个值?
【问题讨论】:
-
您必须向我们展示您的服务器代码,以便我们识别您在那里做错了什么。看起来您可能有一些重复的中间件都设置了 CORS 标头。
-
@jfriend00 我实际上是通过删除我的 nginx 文件中的 add_header 来解决它的。我可以知道这是否是解决它的好方法吗?
-
好的,您可以编写自己的答案(如果您认为该答案对其他人有用),也可以删除您的问题。它不应该只是在这里闲逛而没有答案。
-
@jfriend00 我以为我解决了它,所以在我的支持 ngnix 站点的文件夹中,有一个 add_header 允许 cors。但是,如果通过注释掉它,套接字会正确连接,但是我的 API 会有 CORS 问题……上帝……救救我……哈哈
标签: node.js nginx socket.io socket.io-redis