How to Make GET, POST, and HEAD Requests Using Ajax

See Ajax: Tips and Tricks for similar articles.

Although the HTTP specification identifies several methods of HTTP requests, the most commonly supported (and used) methods are GET, POST, and HEAD.

The HEAD Method

The HEAD method is the least commonly used of the three. Its purpose is to return the meta-information contained in the HTTP headers. To make a HEAD request, follow these two steps:

  1. Create a call to the open() method and pass in "HEAD" as the first argument and a URL as the second argument.
    xmlhttp.open("HEAD","http://www.example.com/Demo?FirstName=Chris&LastName=Minnick");
  2. Send the request.
    xmlhttp.send();

The GET Method

The GET method is used to send information to the server as part of the URL. To make a GET request, follow these two steps:

  1. Create a call to the open() method and pass in "GET" as the first argument and a URL, with name-value pairs passed as a querystring, as the second argument.
    xmlhttp.open("GET","http://www.example.com/Demo?FirstName=Chris&LastName=Minnick");
  2. Send the request.
    xmlhttp.send();

The POST Method

The POST method is used to send information as an enclosed entity. To make a POST request, follow these three steps:

  1. Create a call to the open() method and pass in "POST" as the first argument and a URL as the second argument.
    xmlhttp.open("POST","http://www.example.com/Demo");
  2. Set the content type to "application/x-www-form-urlencoded;charset=UTF-8");
    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
  3. Send the request, passing in name-value pairs through the send() method.
    xmlhttp.send("FirstName=Chris&LastName=Minnick");

Related Articles

  1. How to Make a Cross-origin Ajax Request
  2. How to Create a Login Form with Ajax
  3. How to Set Up Automatic Session Timeout with Ajax
  4. How to Use the Callback Function in Ajax
  5. How to Develop a Web Application with Ajax
  6. How to Make GET, POST, and HEAD Requests Using Ajax (this article)
  7. How to Use the jQuery ajax() Method
  8. How to Create a Lookup Form with Ajax
  9. How to Create a Slideshow with Ajax
  10. How to Handle the Response from the Server in Ajax
  11. How to Set Up for Ajax Training on Windows
  12. Inline Editing Using Ajax
  13. How to Create a Navigable Table with Ajax