Open In App

AJAX XMLHttpRequest Object

XMLHttpRequest is an object that is used to send a request to the webserver for exchanging data or transferring and manipulating to it and from the server behind the scenes. You can use the received data to update the data present on the web page without even reloading the page.

Below is the complete syntax to use XMLHttpRequest object.



Syntax:

At first, you have to invoke the XMLHttpRequest() method as shown below.



var variable_name = new XMLHttpRequest();

After calling the XMLHttpRequest() method you have to define a callback function that will trigger after getting the response. 

variable_name.onload = function () {
    // Content of callback function
    // after getting the response
}

Sending a request using the open()  and send() methods as shown below.

variable_name.open("GET", "textFile.txt");
variable_name.send();

The XMLHttpRequest object has different functions and properties that are listed below.

XMLHttpRequest object methods:

Example: Below example illustrates the use of XMLHttpRequest() object methods.




<!DOCTYPE html>
<html>
  
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <title>Ready states of a request in ajax</title>
  
    <style>
        #container {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
        }
    </style>
</head>
  
<body>
    <div id="container">
        <h1>Hey Geek,</h1>
        <h3>Welcome to GeeksforGeek!</h3>
        <button type="button" onclick="stateChange()">
            Update Text
        </button>
    </div>
  
    <script>
        function stateChange() {
            var state = new XMLHttpRequest();
            state.onload = function () {
                document.getElementById("container")
                    .innerHTML = state.getAllResponseHeaders();
            }
            state.open("GET", "gfgInfo.txt", true);
            state.send();
        }
    </script>
</body>
  
</html>

Output:

 

XMLHttpRequest object properties:

Example: Below example illustrates the use of XMLHttpRequest() object properties.




<!DOCTYPE html>
<html>
  
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
          
    <title>Ready states of a request in ajax.</title>
      
    <style>
        #container {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
        }
    </style>
</head>
  
<body>
    <div id="container">
        <h1>Hey Geek,</h1>
        <h3>Welcome to GeeksforGeek!</h3>
        <button type="button" onclick="stateChange()">
            Update Text
        </button>
    </div>
  
    <script>
        function stateChange() {
            var state = new XMLHttpRequest();
            state.onload = function () {
                if (this.readyState == 4) {
                    document.getElementById("container").innerHTML =
                        this.responseText + "Status Text: " 
                        + this.statusText;
                }
            };
            state.open("GET", "gfgInfo.txt", true);
            state.send();
        }
    </script>
</body>
  
</html>

Output:

 


Article Tags :