admin管理员组

文章数量:1208155

So I have the below code:

    var formData = new FormData();  
    formData.append("title", document.getElementById("title").value);  
    formData.append("html",my_html);  

    var xhr = new XMLHttpRequest();  
    xhr.open("POST", "");  
    xhr.send(formData); 
    xhr.onreadystatechange = function() { 
      // If the request completed, close the extension popup
      if (req.readyState == 4)
        if (req.status == 200) window.close();
    };

The server is supposed to send back a response in JSON format. How do I retrieve and store that in a variable?

So I have the below code:

    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();
    };

The server is supposed to send back a response in JSON format. How do I retrieve and store that in a variable?

Share Improve this question edited Aug 22, 2018 at 16:51 beaver 5271 gold badge9 silver badges21 bronze badges asked Oct 6, 2011 at 12:17 MaxMax 4,4086 gold badges40 silver badges57 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 12

If the answer is in JSON, you have the result in the responseText attribute.

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

For more details, view: XMLHttpRequest

Just use xhr.responseText to get the response of the request. You can also use xhr.responseXML to retreive a DOM-compatible document object of the response, that means you can access it like document.

Source: http://developer.apple.com/internet/webcontent/xmlhttpreq.html

Your response is in xhr.responseText.

Check this: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest

本文标签: javascriptRetrieving the Post Request response and storing into a variableStack Overflow