Loading Please Wait...
The XMLHttpRequest object is used to receive response data return by the server of an Ajax request.
Method | Description |
---|---|
getAllResponseHeaders() | Returns all the header information from the server resource |
getResponseHeader() | Returns specific header information from the server resource |
Property | Description |
---|---|
responseXML | get the response data as XML data |
responseText | get the response data as a string |
The getAllResponseHeaders() method returns all header information from the server response.
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
alert(this.getAllResponseHeaders());
}
xhttp.open("GET", "ajax_info.txt");
xhttp.send();
The getResponseHeader() method returns specific header information from the server response.
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
alert(this.getResponseHeader("Content-Type"));
}
xhttp.open("GET", "ajax_info.txt");
xhttp.send();
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.
<DATA>
<COMPANY>
<NAME>Lynxsia IT SOlutions</NAME>
<URL>https://lynxsia.com/</URL>
</COMPANY>
<COMPANY>
<NAME>Google</NAME>
<URL>https://google.com/</URL>
</COMPANY>
</DATA>
function loadDoc() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {myFunction(this);}
xhttp.open("GET", "companies.xml");
xhttp.send();
}
function myFunction(xml) {
const xmlDoc = xml.responseXML;
const x = xmlDoc.getElementsByTagName("COMPANY");
let table="<tr><th>Name</th><th>Url</th></tr>";
for (let i = 0; i < x.length; i++) {
table += "<tr><td>" +
x[i].getElementsByTagName("NAME")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("URL")[0].childNodes[0].nodeValue +
"</td></tr>";
}
document.getElementById("demo").innerHTML = table;
}
The responseText property returns the server response as a JavaScript string, and you can use it accordingly
<?php
// Server code
// DB Connection and CRUD operations
echo "Saved successfully"; //Return as text to browser
?>
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
alert(this.responseText);
}
xhttp.open("GET", "server.php");
xhttp.send();
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved