Request Methods in AJAX:

Now you know that the XMLHttpRequest Object is use to interchange or exchange data to and from a server.

But the main question is that “How can we send data to a Server?”. So in AJAX we can send a request to a server with the help of some methods they are as follows.

  • open()
  • setRequestHeader()
  • send()
  • abort()

Open Method:

The open method used in AJAX to send a request through XMLHttpRequest Object to the server. There are four different ways to use Open method and they are as follow:

Syntax:

open(method,url)

open(method,url,async)

open(method,url,async,username)

open(method,url,async,username,password)

You can see that there are four ways to use open methods and all methods have different parameters.

The first one method is necessary and you have to pass “GET” or “POST” method type in first parameter and in second parameter you have to pass the path of your file which you want to open.

In the second method you can see third parameter “async” which will specifies that the request should be handled asynchronously or not. In place of “async” parameter you can pass “true” or “false”. TRUE means the script will not wait for the response and FALSE means the script will wait for response before continuing script processing.

In the fourth and fifth method you can see the username and password parameters which may be used for authorization and authentication if required.

Note:

The “method”,”url” and “async” parameters are important and “username” and “password” are optional.

Example:

xmlhttp.open(“GET”,”report_data.xml”,true);

xmlhttp.open(“GET”,”sensitive_data.xml”,false);

xmlhttp.open(“POST”,”saveData”,true,”myUserName”,”somePassord”);

setRequestHeader Method:

The setRequestHeader method is use to add headers to the request. It will invoke when request is successfully initialized.

Syntax:

setRequsetHeader(name,value);

The name parameter specifies the name of the header and the value specifies the value of that header.

Example:

xmlhttp.setRequestHeader(‘X-Requested-With’, ‘XMLHttpRequest’);

Send Method:

The send method is use to send an HTTP request to the server. It is must to perform send method of the XMLHttpRequest.

Syntax:

send(content);

You can see that there is only one parameter in send method and it will contain the content that will be sending with the request.

Example:

xmlhttp.send(null); //Request with no data in request body; Mostly used in GET requests.

xmlhttp.send( {“id”:”23423″} ); //Request with data in request body; Mostly used in POST/ PUT requests

Abort Method:

The abort method cancels the request. This will only do when the “readyState” of the XHR object has not become “4”. This method always ensures that the callback method does not get invoked in any asynchronous request.

Syntax:

abort();

In abort method you don’t have to pass parameter.

Example:

xmlhttp.abort();

Note:

Please don’t be confused with “readyState” we will cover it in our next topic.

Leave a Reply

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