从不同服务器连接socket.io时出错

error in connection the socket.io from different server

本文关键字:io 出错 socket 连接 服务器      更新时间:2023-09-26

我正在制作一个移动应用程序,将node.js REST API作为后端,Angular作为前端,这两个应用程序都将位于不同的服务器上。angular应用程序稍后将与phonegap连接,使其成为混合应用程序。我正在使用socket.io,并尝试将前端连接到后端以建立一个内置的聊天。

当我试图通过连接插座时,我遇到了一个错误

io.connect("(http://localhost:3000/")

XMLHttpRequest无法加载

http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1412172739903-3

当凭据标志为true时,不能在"Access Control Allow Origin"标头中使用通配符"*"。始发

'http://localhost:1235' 

因此不允许访问。

我建议使用https://www.npmjs.org/package/cors

此模块启用http://en.wikipedia.org/wiki/Cross-origin_resource_sharing在您的应用程序上

在NodeJS服务器端代码中(假设您在后端使用ExpressJS),您是否添加了

res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");

此网站可能会帮助您:在服务器端中启用cors