Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving the Post Request response and storing into 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?

like image 881
Max Avatar asked Oct 06 '11 12:10

Max


2 Answers

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

like image 140
marcocamejo Avatar answered Oct 13 '22 04:10

marcocamejo


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

like image 42
1' OR 1 -- Avatar answered Oct 13 '22 04:10

1' OR 1 --