我在端口5000上的node.js服务器上设置了CORS,如下所示:

var app = express();

app.use(cors()); //normal CORS

app.options('*', cors()); //preflight

app.post('/connect', function(req, res) {

console.log("Connection request received !")

});

var port = 5000;

app.listen(port, function () {

console.log('Listening on port '+port);

});

我现在正试图在我的硬盘打开的静态网页中使用JQuery发送AJAX POST请求:

var xhr = $.post({

url: 'http://localhost:5000/connect',

complete: function() {

console.log("done !")

},

crossDomain: true

});

xhr.fail(function(xhr, status, error){

alert(error)

})

complete永远不会调用该函数,我得到的唯一警报是来自XHR fail处理程序的警报,包含以下错误:

NS_ERROR_DOM_BAD_URI: Access to restricted URI denied

我认为CORS配置得很好,我错过了什么?

编辑:对于我的情况,我能找到的最佳解决方案是从服务器发送页面:

app.use(express.static('web'))

app.get("/", function(req, res) {

res.sendFile(__dirname + '/web/HTML/index.html');

});

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐