2011-10-06 50 views
3

所以我有下面的代码:检索Post Request响应并存储到变量中?

  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格式的响应。 如何检索并将其存储在变量中? 任何人都可以帮忙吗?

回答

5

如果答案是JSON,你有结果的responseText的属性。

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

欲了解更多详细信息,查看:XMLHttpRequest

相关问题