Clean β’ Professional
When an AJAX request is sent to a server, the server responds with data. The XMLHttpRequest object provides properties and methods to access this response.
responseText
Example:
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if(xhttp.readyState === 4 && xhttp.status === 200) {
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
responseXML
getElementsByTagName().Example:
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if(xhttp.readyState === 4 && xhttp.status === 200) {
const xmlDoc = xhttp.responseXML;
const artists = xmlDoc.getElementsByTagName("ARTIST");
let txt = "";
for(let i = 0; i < artists.length; i++) {
txt += artists[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
};
xhttp.open("GET", "cd_catalog.xml", true);
xhttp.send();
getResponseHeader(headerName)
Content-Type, Last-Modified, etc.Example:
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("demo").innerHTML =
this.getResponseHeader("Last-Modified");
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
getAllResponseHeaders()
\\r\\n).Example:
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("demo").innerHTML =
this.getAllResponseHeaders();
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();Β