繁体   English   中英

检索Post Request响应并存储到变量中?

[英]Retrieving the Post Request response and storing into a variable?

所以我有以下代码:

    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

您的回复是xhr.responseText

请查看: https//developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest

只需使用xhr.responseText来获取请求的响应。 您还可以使用xhr.responseXML来检索响应的DOM兼容文档对象,这意味着您可以像document一样访问它。

资料来源: http//developer.apple.com/internet/webcontent/xmlhttpreq.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM