JS AsyncLesson #279

AJAX - Server Response

The responseText property returns the server response as a JavaScript string, and you can use it accordingly:

Server Response Properties

PropertyDescription
responseTextget the response data as a string
responseXMLget the response data as XML data

The responseText Property

The responseText property returns the server response as a JavaScript string, and you can use it accordingly:

Example

Example Code

javascript
Try it Yourself
document.getElementById("demo").innerHTML = xhttp.responseText;

The responseXML Property

The XMLHttpRequest object has an in-built XML parser.

The responseXML property returns the server response as an XML DOM object.

Using this property you can parse the response as an XML DOM object:

Example

Example Code

javascript
Try it Yourself
const xmlDoc = xhttp.responseXML;

const x = xmlDoc.getElementsByTagName("ARTIST");

let txt = "";

for (let i = 0; i  ";

  }

document.getElementById("demo").innerHTML = txt;

xhttp.open("GET",
 "cd_catalog.xml");
xhttp.send();

Server Response Methods

MethodDescription
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

Example Code

javascript
Try it Yourself
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

Example Code

javascript
Try it Yourself
const xhttp = new XMLHttpRequest();

xhttp.onload = function() {

    document.getElementById("demo").innerHTML =

    this.getResponseHeader("Last-Modified");

}

xhttp.open("GET", "ajax_info.txt");

xhttp.send();

Interactive Code Examples

JS Example 1

javascript
Try it Yourself
Interactive runnable JavaScript example from EA Academy.
Live Output Preview:

JS Example 2

javascript
Try it Yourself
Interactive runnable JavaScript example from EA Academy.
Live Output Preview:

JS Example 3

javascript
Try it Yourself
Interactive runnable JavaScript example from EA Academy.
Live Output Preview:

JS Example 4

javascript
Try it Yourself
Interactive runnable JavaScript example from EA Academy.
Live Output Preview: