问题内容类型更改为Json(XMLHttpRequest)

问题描述 投票:0回答:1
    let data = JSON.stringify({
    "from": "testnumber",
    "to": "testnumber",
    "text": "Test SMS."
});

let XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

let xhr = new XMLHttpRequest();

xhr.withCredentials = false;

xhr.addEventListener("readystatechange", function () {
   if (this.readyState === this.DONE) {
        console.log(this.responseText);
    }
});


xhr.open("POST", "https://thaina.free.beeceptor.com", true);
xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
xhr.setRequestHeader('accept', 'application/json');

xhr.send(data);

无关紧要,我执行的内容类型始终是文本/纯文本。您可能有什么建议吗?

带有此代码的蜂鸣器测试

“使用此代码的蜂鸣器测试”

javascript json xmlhttprequest content-type
1个回答
0
投票

node XMLHttpRequest似乎有点越野车

使用xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');

然后它将起作用

导致该问题的模块中的代码为

  if (!headers["Content-Type"]) {
    headers["Content-Type"] = "text/plain;charset=UTF-8";
  }

[在您设置的所有标头之后添加Content-Type-因为您设置了Content-type,所以这是一个不同的标头

代码应该是

  if (!headersCase["content-type"]) {
    headers["Content-Type"] = "text/plain;charset=UTF-8";
  }
© www.soinside.com 2019 - 2024. All rights reserved.