How to Use XMLHttpRequest in JavaScript

I am going to write a series of articles on "XMLHttpRequest". This is my first article on how to use "XMLHttpRequest". Please provide feedback for me about this article.

What is XMLHttpRequest?

  1. The XMLHttpRequest object is useful if you want to send and retrieve data from a server without reloading the current page.
  2. Typically used for retrieving XML files or pure text content from a server.
  3. XMLHttpRequest provides an easy way to retrieve data at a URL.
  4. Despite its name, XMLHttpRequest can be used to retrieve any type of data, not just XML, and it supports protocols other than HTTP (including file and FTP).
  5. JavaScript code loaded in the client browser can request additional data from the Web server using the XMLHttpRequest object.

How to use XMLHttpRequest?

  1. XMLHttpRequest makes sending HTTP requests very easy.
  2. You simply create an instance of the object, open a URL, and send the request.
  3. The HTTP status of the result, as well as the result's contents, is available in the request object when the transaction is completed.
  4. This page outlines some of the common and even slightly obscure use cases for this powerful JavaScript object.
  5. In short, first initialize an XMLHttpRequest object with the open method, then specify the necessary request headers with the setRequestHeader method and finally send the request with the send method.

Example

  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head runat="server">  
  3.     <title></title>  
  4.    
  5.     <script type="text/javascript" language="javascript">  
  6.         function GetDataFromAotherPage() {  
  7.             var objReq = new XMLHttpRequest();  
  8.             objReq.open("GET""Default2.aspx"false);  
  9.             objReq.send(null);  
  10.             alert(objReq.responseText);  
  11.             return false;  
  12.         }  
  13.     </script>  
  14.    
  15. </head>  
  16. <body>  
  17.     <form id="form1" runat="server">  
  18.     <div>  
  19.         <asp:Button ID="btnClickMe" runat="server" OnClientClick="return GetDataFromAotherPage();"  
  20.             Text="Click Me" />  
  21.     </div>  
  22.     </form>  
  23. </body>  
  24. </html>

Description

  1. var objReq = new XMLHttpRequest();

    This will create an "XMLHttpRequest" object.

  2. objReq.open("GET", "Default2.aspx", false);

    The Open() method will initialize an HTTP request for sending. The syntax of the "open" method is as follows:

    objReq.open(method, URL, async, user, password);

    • Method: Required. String that specifies the HTTP method (GET, POST, DELETE, HEAD, OPTIONS, PUT) to use. This parameter is case-insensitive.
    • URL: Required. String that specifies the URL where the request needs to be sent. The specified URL must be in the same domain as the current document.
    • Async: Optional. Boolean that specifies whether the request needs to be handled asynchronously or not. Two values are true and false.

      True (asynchronous): If you use an asynchronous data transfer then register the onreadystatechange event before you send the request. The onreadystatechange event fires every time the state of the request changes.
      False (synchronous): In case of synchronous data transfers, the send method does not return until the response arrives. Do not use this type of data transfer if you do not want to keep the user waiting.
    • User: Optional. String that specifies the name of the user for authentication. The default is an empty string.
    • Password: Optional. String that specifies the password of the user for authentication. The default is an empty string.
  3. objReq.send(null);

    The Send() method will send an HTTP request to the server. The syntax for the "send" method is as follows:

    objReq.send(messageBody);

    • MessageBody: Optional. String that specifies the body of the request. The default is an empty string. In the case of GET requests (see the method parameter of the open method) this value is ignored, the request body is always empty.
  4. objReq.responseText

    Response Text will return the body of the server's response as a string. This property is read-only. 

Output

  1. If you run the code above or the attachment code, the page will be displayed with a button on it.

  2. When you click on the button a pop-up window will be opened with the following output:

    XMLHttpRequest.jpg

I hope you will enjoy the article. Thank you.

Up Next
    Ebook Download
    View all
    Learn
    View all