【问题标题】:Retrieving the Post Request response and storing into a variable?检索发布请求响应并存储到变量中?
【发布时间】:2011-10-06 12:17:35
【问题描述】:

所以我有以下代码:

    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 格式发回响应。 如何检索并将其存储在变量中?

【问题讨论】:

    标签: javascript html http-post


    【解决方案1】:

    如果答案是 JSON 格式,则结果会在 responseText 属性中。

    if (xhr.readyState == 4)
      if (xhr.status == 200)
        var json_data = xhr.responseText; 
    

    更多详情请查看:XMLHttpRequest

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-06-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多