Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Ajax Introduction

JS Ajax Introduction

Ajax is used to make request to web servers and update the website without reloading.

What is AJAX?

Asynchronous JavaScript And XML or Ajax is not a programming language.

AJAX just uses a combination of:

  • A browser built-in XMLHttpRequest object (to request data from a web server)
  • JavaScript and HTML DOM (to display or use the data)

AJAX is a misleading name. AJAX applications might use XML to transport data, but it is equally common to transport data as plain text or JSON text.

AJAX allows web pages to be updated asynchronously by exchanging data with a web server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.

How AJAX Works
Ajax Working
AJAX Example
					 
        
          <!DOCTYPE html>
          <html>
            <body>
              <button type="button" id="ajaxButton">Change Content</button>

              <script>
                let httpRequest;
                let button = document.getElementById("ajaxButton");
                button.addEventListener("click", makeRequest);

                function makeRequest() {
                  httpRequest = new XMLHttpRequest();

                  if (!httpRequest) {
                    alert("Giving up :( Cannot create an XMLHTTP instance");
                    return false;
                  }
                  httpRequest.onreadystatechange = alertContents;
                  httpRequest.open("GET", "test.html");
                  httpRequest.send();
                }

                function alertContents() {
                  if (httpRequest.readyState === XMLHttpRequest.DONE) {
                    if (httpRequest.status === 200) {
                      alert(httpRequest.responseText);
                    } else {
                      alert("There was a problem with the request.");
                    }
                  }
                }
              </script>
            </body>
          </html>
        
      
Modern Browsers (Fetch API)

Modern Browsers can use Fetch API instead of the XMLHttpRequest Object.

The Fetch API interface allows web browser to make HTTP requests to web servers.

If you use the XMLHttpRequest Object, Fetch can do the same in a simpler way.

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