所以我有以下代码:
var formData = new FormData();
formData.append("title", document.getElementById("title").value);
formData.append("html",my_html);
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://www.mywebsite.com/index");
xhr.send(formData);
xhr.onreadystatechange = function() {
// If the request completed, close the extension popup
if (req.readyState == 4)
if (req.status == 200) window.close();
};
服务器应该以 JSON 格式发回响应。 我如何检索它并将其存储在变量中?
最佳答案
如果答案是 JSON 格式,则结果在 responseText 属性中。
if (xhr.readyState == 4)
if (xhr.status == 200)
var json_data = xhr.responseText;
更多详情,查看:XMLHttpRequest
关于javascript - 检索发布请求响应并存储到变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7674134/