Javascript HTTP请求,缺少内容类型
我已经创建了XMLHttpRequest:Javascript HTTP请求,缺少内容类型,javascript,node.js,https,cors,Javascript,Node.js,Https,Cors,我已经创建了XMLHttpRequest: var body = "{cos: test}"; this.XHRhttp.open("POST", "https://10.222.1.170:8600"); this.XHRhttp.onreadystatechange = this.readyState.bind(this); this.XHRhttp.setRequestHeader("accept", "*/*"); this.XHRhtt
var body = "{cos: test}";
this.XHRhttp.open("POST", "https://10.222.1.170:8600");
this.XHRhttp.onreadystatechange = this.readyState.bind(this);
this.XHRhttp.setRequestHeader("accept", "*/*");
this.XHRhttp.setRequestHeader("content-type", "application/json");
this.XHRhttp.send(body);
当我尝试发送内容类型头时,服务器的响应是该头不存在
但是,当我删除内容类型时,服务器会收到“text/plain;charset=UTF8”。
为什么我的内容类型标题没有被发送
服务器端:
var securedServer = require("https").createServer(authOption, function (req, res) {
.......
if(req.headers["content-type"] !== "application/json")
{
opt.statusCode = 405;
opt.statusMsg = "Wrong Content-Type";
opt.body = "Only application/json Content-Type allowed";
RespondToClient(res, opt);
return;
}
.............
}
function RespondToClient(res, opt)
{
res.writeHead(opt.statusCode, opt.statusMsg, {
"Access-Control-Allow-Origin": opt.headerOrigin,
"Access-Control-Allow-Methods": "POST",
"Access-Control-Allow-Headers": "content-type",
'Content-Type': opt.contentType,
"Content-Length": opt.body.length,
"Connection": "Close"
});
res.shouldKeepAlive = false;
res.end(opt.body, "utf8");
}
您是否使用了任何东西来验证标头是否确实被发送了?当我发送正文时,作为非字符串化对象类型,服务器在请求“Access Control Allow Headers”中有:“content type”,但“json/application”我没有看到。不,我不验证这一点,只调试服务器代码并检查请求和响应对象。