Search code examples
javascriptrequestresponse

Get response from server with Javascript after Javascript request


My Javascript function request to a aspx page. İts Code:

 var xhr = ("XMLHttpRequest" in window) ? new XMLHttpRequest() : new ActiveXObject("Msxml3.XMLHTTP");
    xhr.open("GET", = 'http://www.example.net/abc.aspx', true);
    xhr.send(""); 

After this request I want to send a response back from this page and catch it on the client side. How do I do that?


Solution

  • To get the response from XMLHttpRequest in asynchronous mode (third parameter is true for the open() method) you have to set the onreadystatechange property to a callback function. This function will be called back when the response is ready at the browser:

    xhr.open("GET", 'http://www.example.net/abc.aspx', true);
    xhr.onreadystatechange = function() {
      if (xhr.readyState === 4)  { 
        var serverResponse = xhr.responseText;
      }
    };
    xhr.send(null);
    

    You may want to check out the following article for further reading on the topic: