将内容类型更改为 Json (XMLHttpRequest) 的问题
Issues to have content type changed to Json (XMLHttpRequest)
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);
不管我执行的内容类型始终是 text/plain 的更改。您有什么建议吗?
使用此代码进行蜂鸣器测试
node XMLHttpRequest
好像有点bug
使用xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
然后就可以了
该模块中导致问题的代码是
if (!headers["Content-Type"]) {
headers["Content-Type"] = "text/plain;charset=UTF-8";
}
在您设置的所有 header 之后添加 Content-Type - 因为您设置了 Content-type
这是一个不同的 header
代码应该是
if (!headersCase["content-type"]) {
headers["Content-Type"] = "text/plain;charset=UTF-8";
}
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);
不管我执行的内容类型始终是 text/plain 的更改。您有什么建议吗?
使用此代码进行蜂鸣器测试
node XMLHttpRequest
好像有点bug
使用xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
然后就可以了
该模块中导致问题的代码是
if (!headers["Content-Type"]) {
headers["Content-Type"] = "text/plain;charset=UTF-8";
}
在您设置的所有 header 之后添加 Content-Type - 因为您设置了 Content-type
这是一个不同的 header
代码应该是
if (!headersCase["content-type"]) {
headers["Content-Type"] = "text/plain;charset=UTF-8";
}