Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Ajax Response

JS Ajax Response

The XMLHttpRequest object is used to receive response data return by the server of an Ajax request.

Server Response Methods
Method Description
getAllResponseHeaders() Returns all the header information from the server resource
getResponseHeader() Returns specific header information from the server resource
Server Response Properties
Property Description
responseXML get the response data as XML data
responseText get the response data as a string
The getAllResponseHeaders() Method

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

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 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.

The companies.xml File
					 
        
          <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 responseXML Property

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

The server.php File
					 
        
          <?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:

Report Us

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 Us
Ads
Logo
Lynxsia IT Solutions

We 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..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved