我在端口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');
});