ajax接口请求成功报cors,POST AJAX请求被拒绝-CORS?

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

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值