HTTP请求,内容类型丢失

时间:2015-07-10 14:43:23

标签: 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.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");
}

0 个答案:

没有答案