【发布时间】:2018-06-13 23:45:57
【问题描述】:
我正在尝试从我的 cordova 应用程序访问 Node.js socket.io 服务器。 然后我收到以下错误:
Failed to load https://www.ride4you.co.il/socket.io/?EIO=3&transport=polling&t=M30S1z4: 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'. Origin 'http://127.0.0.1:8080' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
这是客户端(应用程序或本地主机)的连接代码:
<script src="https://www.ride4you.co.il/socket.io/socket.io.js">
</script>
<script type="text/javascript">
var socket = io("https://www.ride4you.co.il");
socket.on("disconnect", function() {
console.log("Disconnected");
socket = io();
console.log("Connected");
});
</script>
当我在实际域中尝试此代码时,没有错误,一切正常。但是从不是原始域的应用程序中,有一个块。
我已尝试移除此块,并允许每个源使用 app.js(主 node.js 文件)中的下一个代码访问此套接字:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
global.io = require('socket.io').listen(server);
io.set('origins', '*:*');
但仍然出现同样的错误。
我知道有一个允许特定域的选项,但我正在使用一个没有域的 cordova 应用程序 - 所以我认为每个人都应该允许,不是吗?
谢谢。
【问题讨论】: