简体   繁体   English

响应中的 Access-Control-Allow-Origin 标头不能是通配符 '*'... Socket.io、NodeJs、ReactJs

[英]Access-Control-Allow-Origin header in the response must not be the wildcard '*'... Socket.io, NodeJs, ReactJs

I'm new to socket.io .我是socket.io 的新手。 I have following code in my NodeJs/Express server我的 NodeJs/Express 服务器中有以下代码

const http = require('http');
const socketIo = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);

io.on('connection', socket => {
    console.log('New client connected');
    socket.on('disconnect', () => console.log('Client disconnected'));
    socket.emit('FromAPI', 'hello');
});
//My port here is 8848
app.listen(app.get('port'), () => {
    logger.log('info', `Server started at :${app.get('port')}`);
});

And following code in Client React app and my react app is hosted in port 3000 as http://localhost:3000以下是Client React 应用程序中的代码,我的React 应用程序托管在端口 3000 中作为http://localhost:3000

import socketIO from 'socket.io-client';
componentDidMount() {
    const endPoint = 'http://127.0.0.1:8848';
    const socket = socketIO(endPoint);
    socket.on('FromAPI', data => console.log(data));

  }

With this code i got the following error on the browser console:使用此代码,我在浏览器控制台上收到以下错误:

polling-xhr.js:263 GET http://127.0.0.1:8848/socket.io/?EIO=3&transport=polling&t=MMT-4kl 405 (Method Not Allowed) Failed to load http://127.0.0.1:8848/socket.io/?EIO=3&transport=polling&t=MMT-Aso : The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. polling-xhr.js:263 GET http://127.0.0.1:8848/socket.io/?EIO=3&transport=polling&t=MMT-4kl 405 (Method Not Allowed) 无法加载http://127.0.0.1:8848 /socket.io/?EIO=3&transport=polling&t=MMT-Aso :当请求的凭据模式为“include”时,响应中“Access-Control-Allow-Origin”标头的值不能是通配符“*” . Origin ' http://localhost:3000 ' is therefore not allowed access.因此,不允许访问 Origin ' http://localhost:3000 '。 The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute. XMLHttpRequest 发起的请求的凭证模式由 withCredentials 属性控制。

Anyone please help me with this problem.任何人都请帮助我解决这个问题。

According to MDN Web Doc , you can add the host urls.根据 MDN Web Doc ,您可以添加主机网址。

solution 1解决方案1

There is probably similar question answered already.可能已经回答了类似的问题。 If that does not work for you try solution 2.如果这对您不起作用,请尝试解决方案 2。

Here is the stackoverflow answer : Solution 1这是stackoverflow的答案: 解决方案1

Solution 2解决方案2

Limiting the possible Access-Control-Allow-Origin values to a set of allowed origins requires code on the server side to check the value of the Origin request header, compare that to a list of allowed origins, and then if the Origin value is in the list, to set the Access-Control-Allow-Origin value to the same value as the Origin value.将可能的 Access-Control-Allow-Origin 值限制为一组允许的来源需要服务器端的代码检查 Origin 请求标头的值,将其与允许的来源列表进行比较,然后如果 Origin 值在列表,将 Access-Control-Allow-Origin 值设置为与 Origin 值相同的值。

Access-Control-Allow-Origin: https://abc.xyz.gi访问控制允许来源: https : //abc.xyz.gi

so in your case所以在你的情况下

Access-Control-Allow-Origin: https://localhost访问控制允许来源: https://localhost

or或者

Access-Control-Allow-Origin: https://127.0.0.1访问控制允许来源: https : //127.0.0.1

Further relevant QA read more on stackeoverflow进一步相关的 QA 阅读更多关于 stackeoverflow

暂无
暂无

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

相关问题 Socket.io 和 express 应用程序由于 CORS 错误而无法连接:“'Access-Control-Allow-Origin' header 的值不能是通配符 '*'” - Socket.io and express app not connecting due to CORS error: “The value of the 'Access-Control-Allow-Origin' header must not be the wildcard '*'” Socket.io-Access-Control-Allow-Origin不允许的来源 - Socket.io - Origin not allowed by Access-Control-Allow-Origin 响应中 'Access-Control-Allow-Origin' header 的值不能是通配符 '*' 但它在我的服务器中不是通配符 - The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' but its not wildcard in my server socket.io, 'Access-Control-Allow-Origin' 错误 - socket.io, 'Access-Control-Allow-Origin' error Java服务器上具有Socket.IO的“访问控制允许来源” - 'Access-Control-Allow-Origin' with Socket.IO on Java Server 当请求的凭据模式为“ include”时,响应中“ Access-Control-Allow-Origin”标头的值不得为通配符“ *” - Value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include' 如何修复“响应中的 'Access-Control-Allow-Origin' header 的值不能是通配符 '*'” - How to fix "The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' " 当请求的凭据模式为“包含”时,响应中的 Access-Control-Allow-Origin 标头不得为通配符“*” - Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include' Meteor 和套接字 IO - 不存在“访问控制允许来源” header - Meteor and Socket IO - No 'Access-Control-Allow-Origin' header is present Socket.io没有'Access-Control-Allow-Origin'标头出现在请求的资源上。 因此不允许Origin'http:// localhost'访问 - Socket.io No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM