corspost请求失败_CORS收发post异步请求在nodejs下失败问题

想用cors收发异步请求,我是这样做的:

先造一下假数据尝试一下:

var mydata = {

id:timestamp,

pass:"14444",

title:"24444",

tag:"34444",

content:"544444"

};

var XHR = new XMLHttpRequest();

XHR.open("POST", "http://localhost:8090/savemycollec");

XHR.setRequestHeader('Content-Type', 'application/json');

XHR.onreadystatechange = handler;

XHR.send(JSON.stringify(mydata));

服务端:

res.header('Access-Control-Allow-Origin', '*');

res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept');

res.header('Access-Control-Allow-Methods', 'POST,GET,DELETE,PUT');

console.log(req.body);

console.log(req.query);

res.end();

检测值,

结果发现:

仍然是报错:

XMLHttpRequest cannot load http://localhost:8090/savemycollec. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.

貌似是不能直接接受json格式的请求?

如果把请求中setRequestHeader去掉的话就不报错了但是传空值(收不到数据)

请相关经验人士指点一二,

res.set('Access-Control-Allow-Headers', 'Content-Type');

res.set('Access-Control-Allow-Methods', 'GET, POST, DELETE, PUT');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值