Alex Rivera | Logout

Retrieving the Post Request response and storing into a variable?

Asked 2011-10-06T12:17:35.410
11

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?

Edit
Report

1 Answer

1

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

answered 2011-10-06T12:30:28.987

Your Answer