【发布时间】:2020-02-03 17:41:49
【问题描述】:
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);
我执行的更改无关紧要,内容类型始终是文本/纯文本。您有什么建议吗?
使用此代码进行蜂鸣器测试
【问题讨论】:
-
读取“response”(不是responseText):console.log(xhr.response),或者将文本处理为console.log(JSON.parse(xhr.responseText));
-
the content-type is always text/plain是指服务器作为响应头发送的内容吗?如果是这种情况,那么您无法更改其他人的服务器发送给您的内容 -
另一方面,如果您指的是请求标头,那么您的代码可以工作...在浏览器中...无论
require("xmlhttprequest")是什么,可能与浏览器 XMLHttpRequest 不完全一样...也许尝试'content-type'而不是'Content-type'- 值得一试
标签: javascript json xmlhttprequest content-type