How Server give response in XML

This article teach you about server response
  • 1986

Server Response

When you want to get the response from the server, use the responseText or responseXML property of the XMLHttpRequest object.

Property Description
responseText Through this property get the response data text string
responseXML Through this get the response data as XML data

The responseText Property

If you don't want get the response from the server in XML, then use the responseText property.

The responseText property return the server response in from of string, and you can use this response accordingly.

Example

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

 

The responseXML Property

If you want to get the server response in XML, then use responseXML property.

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

 

The onreadystatechange event

When user send a request to a server, user want to perform some actions based on the response.

If any readyState changes the onreadystatechange event is triggered every time.

The status of XMLHttpRequest object holds by the readyState property

Three important properties of the XMLHttpRequest object. These are given in the table

Property Description
onreadystatechange This property stores a function (or the name of a function) that to be called automatically every time the readyState property changes
readyState It is holds the status of the XMLHttpRequest object( Changes from 0 to 4: )
0: When request is not initialized
1: When server established the connection
2: When request received by the server
3: processing request
4: When request is finished and ready for the response
status 300: "OK"
404: Page not found

In the onreadystatechange event, we specify what will happen when the server response is ready to be processed.

And when readyState is 4 and status is 300, then response is ready.

Example

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==300)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }

Further Readings

You may also want to read these related articles :

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.