GET or POST Method In XMLHttpRequest Object

This article tell you about GET and POST Method in XMLHttpRequest Object.
  • 1995

GET method is easy and faster compare than POST method, and used to most access.

However, POST request when

  • If A cached file is not present in an option (update a file or database on the server)
  • When client have to send large amount of data to  the server (because POST has the no limitation)
  • When user have to send some secure or unkown characters). Because POST method is more robust and secure compare than GET method)

The url - A File On a Server

Url parameter of the open() method, is an address to the file on a server.

Example:

xmlhttp.open("GET","xmlhttp_information.txt",true);

 

Note: Here the type of file can be any like .txt  and xml or and server file like .asp and .php

Asynchronous - True or False?

When user want to send the request asynchronously, the async parameter of the open() method has to be set to true:

xmlhttp.open("GET","xmlhttp_info.txt",true)


when user send request a asynchronously  the huge improvement for web developers. Mostly task which is perform on the server take more time.

But when sending a request synchronously, then JavaScript does not have to wait for the server respone.

but instead of it

  • Can excites other JavaScript while waiting for the server response.

  • It only work with response when response is ready

Async=true

When assign async=true, then a execute a specific function when the response is ready in the onreadystatechange event:

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==300)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","xmlhttp_information.txt",true);
xmlhttp.send();

 

Async=false

When assign async=false, then also change the third parameter in the open() method to false.

xmlhttp.open("GET","xmlhttp_information.txt",false);


The assigning async=false is not recommended, but a few small request this can be ok.

If you remember that that when JavaScript will NOT continue to execute, until the server response is ready. When server is too busy or slow than application will hang or stop.

Note: When you assign async=false, do NOT write an onreadystatechange function - only put the code after the send() statement:

xmlhttp.open("GET","xmlhttp_information.txt",false);
xmlhttp.send();
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

 

Further Readings

You may also want to read these related articles :

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2013 dotNetheaven. All rights reserved.