【发布时间】:2019-02-26 01:46:26
【问题描述】:
我正在使用 JSON 和 post 方法向服务器发送数据,但我无法读取来自服务器的响应。这是我的代码:
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://staging.smartenupcs.com/api/v1/licenses/create", true);
xhr.setRequestHeader("Smartenup-API-KEY", "webflow.c407d56c5ab23115af0075+DzDMrMtWZENCoct9Pa7DUA54mIgP8c9o");
var jsonStr = JSON.stringify({
"first_name": "Bla",
"last_name": "Blabla",
"email": "bla@gmail.com",
"product_name": "webflow_essentials",
"order_id": 21811,
"voucher": null
});
xhr.send(jsonStr);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var myObj = JSON.parse(xhr.responseText);
alert(myObj);
}
};
我尝试了很多选择,但没有成功。
希望有人能帮忙,先谢谢了
【问题讨论】:
-
你试过检查什么是“xhr.status”吗?可能不是 200?
-
使用浏览器调试工具检查正在进行的网络调用,看看是否得到了预期的响应。
标签: javascript json xmlhttprequest