AJAX example using javascript

This tutorial shows how to create ajax in simple way without using jquery or any other frameworks. It check first browsers and creates an XMLHttpRequest object depending upon the type of browser. Then we can use as per our requirements whether it is a GET or POST request.
Check type of browser and create XMLHttpRequest object

 

Suppose

 

GET Request Example

A simple GET request can be written as

 

In the above example,
first parameter – type of request
second parameter – url
third parameter – asynchronous or not

The method send() is used to send the data to the server and it is used for only POST request.

In the above example, we may get cached results. So to avoid this we add a unique value to the URL.

 

If we need to send information with the GET request

 

POST Request Example

A simple POST request is written as

 

To POST data, we add an HTTP header with setRequestHeader().
We have to send the data in the send() method.

 

Thanks for your reading. Please do not forget to leave a comment.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.