Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XHR response


May 08, 2021 AJAX


Table of contents


AJAX - Server Response


Because http responses are issued by the service side, and it takes time for the server to respond (such as slow network speeds), we need to monitor the status of the server response before processing.
  • The status line
    xhr.status code, e.g. 200,304,404, etc.;
  • The response body
    xhr.responseText and xhr.responseXML the response body.

For a response from the server, use the responseText or responseText properties of the XMLHttpRequest object. responseXML

Property Describe
responseText Gets the response data in the form of a string.
responseXML Get response data in XML form.



ResponseText property

If the response from the server is not XML, use the responseText

responseText returns a response in the form of a string, so you can use this:

document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

Try it out . . .

Tip: For responseText the responseText property is available only if the readyState responseText to 4 because it indicates that the AJAX request is over!


The responseXML property

If the response from the server is XML and needs to be resolved as an XML object, use the responseXML

Request cd_catalog.xml file and resolve the response:

xmlDoc=xmlhttp.responseXML;
txt="";
x=xmlDoc.getElementsByTagName("ARTIST");
for (i=0;i<x.length;i++)
{
txt=txt + x[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById("myDiv").innerHTML=txt;

Try it out . . .