AJAX - Response
Server Response Properties
Property
Description
responseText
Retrieves the response data as a string.
responseXML
Retrieves the response data as XML.
The responseText Property
The responseText property returns the server's response as a JavaScript string, which can be used accordingly:
Example:
document.getElementById("demo").innerHTML = xhttp.responseText;
The responseXML Property
The XMLHttpRequest object includes an XML parser. The responseXML property returns the server's response as an XML DOM object. This property allows you to parse the response as an XML DOM object.
Example: Request the file cd_catalog.xml and parse the response:
const xmlDoc = xhttp.responseXML;const x = xmlDoc.getElementsByTagName("ARTIST");let txt = "";for (let i = 0; i < x.length; i++) { txt += x[i].childNodes[0].nodeValue + "<br>";}document.getElementById("demo").innerHTML = txt;xhttp.open("GET", "cd_catalog.xml");xhttp.send();
Server Response Methods
Method
Description
getResponseHeader()
Returns specific header information from the server resource.
getAllResponseHeaders()
Returns all the header information from the server resource.
The getAllResponseHeaders() Method
The getAllResponseHeaders() method returns all header information from the server response.
Example:
const xhttp = new XMLHttpRequest();xhttp.onload = function() { document.getElementById("demo").innerHTML = this.getAllResponseHeaders();}xhttp.open("GET", "ajax_info.txt");xhttp.send();
The getResponseHeader() Method
The getResponseHeader() method returns specific header information from the server response.
Example:
const xhttp = new XMLHttpRequest();xhttp.onload = function() { document.getElementById("demo").innerHTML = this.getResponseHeader("Last-Modified");}xhttp.open("GET", "ajax_info.txt");xhttp.send();