2011-10-06 51 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

相關問題