When you use Ajax to access the server without reloading the webpage you have two choices on how to pass the information for therequest to the server. These two options are to use GET or POST.
These are the same two options that you have when passing requests to the server to load a new page with two differences. The first difference is that you are only requesting a small piece of information instead of an entire web page. The second and most noticable difference is that since the Ajax request doesn't appear in the address bar there is nonoticable difference that your visitor will see on their screen when the request is made. Calls made using GET will not expose the fields and their values any there that using POST does not also expose when the call is made from Ajax.
So how should we make the choice as to which of these two alternatives that we should use?
Amistake that some beginners might make is to use GET for most of their calls simply because it is the easier of the two to code. The most noticable difference between GET and POST calls in Ajax is that GETcalls still have the same limit on the amount of data that can be passed as applies when requesting a new page load. The only differenceis that as you are only processing a small amount of data with an Ajax request (or at least that's how you should use it) you are far less likely to run into this length limit from within Ajax to what you are with complete web page loads. A beginner may reserve using POST requests for the few instances where they do need to pass more information that the GET method allows.
The best solution when you have lots of data to pass like that is to make multiple Ajax calls passing a few pieces of information at a time. If you are going to pass huge amounts of data all in the one Ajax call then you would probablybe better off simply reloading the entire page since there will be no significant difference in the processing time when huge amounts of data are involved.
So if the amount of data to be passed isn't a good reason to use for choosing between GET and POST then what should we useto decide which to use? These two methods were in fact set up for entirely different purposes and the differences between how they workare in part due to the difference in what they are intended to be used for. This not only applies to using GET and POST from Ajax but applies to these methods where ever they are used.
The purpose of GET is as its name implies - to GET information. It is intended to be used when you are reading information to display on the page. Browsers will cache the result from a GET request and if the same GET request is made again then they will display the cached result rather than rerunning the entire request. This is not a flaw in the browser processing but is deliberately designed to work that way so as to make GET calls more efficient when the calls are used for their intended purpose. A GETcall is retrieving data to display in the page and data is not expectedto be changed on the server by such a call and so re-requesting thesame data should be expected to obtain the same result.
The POST method is intended to be used where you are updating information on theserver. Such a call is expected to make changes to the data stored onthe server and the results returned from two identical POST calls may very well be completely different from one another since the initialvalues before the second POST call will be different from the initialvalues before the first call because the first call will have updatedat least some of those values. A POST call will therefore always obtain the response from the server rather than keeping a chached copy of theprior response.
So rather than choosing between GET and POSTbased on the amount of data that you are passing in your Ajax call, you should select between them based on what the Ajax call is actually doing. If the call is to retrieve data from the server then use GET. If the value to be retrieved is expected to vary over time as a result of other processes updating it then add a current time parameter to what you are passing in your GET call so that the later calls will not use an earlier cached copy of the result that is no longer correct. If your call is going to write any data at all to the server then use POST.
Infact you should not only use this criteria for selecting between GETand POST for your Ajax calls. You should use this as the criteria for choosing whether to use GET or POST when processing forms on your web page as well.
TRACEBACK: http://javascript.about.com/od/ajax/a/ajaxgp.htm
These are the same two options that you have when passing requests to the server to load a new page with two differences. The first difference is that you are only requesting a small piece of information instead of an entire web page. The second and most noticable difference is that since the Ajax request doesn't appear in the address bar there is nonoticable difference that your visitor will see on their screen when the request is made. Calls made using GET will not expose the fields and their values any there that using POST does not also expose when the call is made from Ajax.
So how should we make the choice as to which of these two alternatives that we should use?
Amistake that some beginners might make is to use GET for most of their calls simply because it is the easier of the two to code. The most noticable difference between GET and POST calls in Ajax is that GETcalls still have the same limit on the amount of data that can be passed as applies when requesting a new page load. The only differenceis that as you are only processing a small amount of data with an Ajax request (or at least that's how you should use it) you are far less likely to run into this length limit from within Ajax to what you are with complete web page loads. A beginner may reserve using POST requests for the few instances where they do need to pass more information that the GET method allows.
The best solution when you have lots of data to pass like that is to make multiple Ajax calls passing a few pieces of information at a time. If you are going to pass huge amounts of data all in the one Ajax call then you would probablybe better off simply reloading the entire page since there will be no significant difference in the processing time when huge amounts of data are involved.
So if the amount of data to be passed isn't a good reason to use for choosing between GET and POST then what should we useto decide which to use? These two methods were in fact set up for entirely different purposes and the differences between how they workare in part due to the difference in what they are intended to be used for. This not only applies to using GET and POST from Ajax but applies to these methods where ever they are used.
The purpose of GET is as its name implies - to GET information. It is intended to be used when you are reading information to display on the page. Browsers will cache the result from a GET request and if the same GET request is made again then they will display the cached result rather than rerunning the entire request. This is not a flaw in the browser processing but is deliberately designed to work that way so as to make GET calls more efficient when the calls are used for their intended purpose. A GETcall is retrieving data to display in the page and data is not expectedto be changed on the server by such a call and so re-requesting thesame data should be expected to obtain the same result.
The POST method is intended to be used where you are updating information on theserver. Such a call is expected to make changes to the data stored onthe server and the results returned from two identical POST calls may very well be completely different from one another since the initialvalues before the second POST call will be different from the initialvalues before the first call because the first call will have updatedat least some of those values. A POST call will therefore always obtain the response from the server rather than keeping a chached copy of theprior response.
So rather than choosing between GET and POSTbased on the amount of data that you are passing in your Ajax call, you should select between them based on what the Ajax call is actually doing. If the call is to retrieve data from the server then use GET. If the value to be retrieved is expected to vary over time as a result of other processes updating it then add a current time parameter to what you are passing in your GET call so that the later calls will not use an earlier cached copy of the result that is no longer correct. If your call is going to write any data at all to the server then use POST.
Infact you should not only use this criteria for selecting between GETand POST for your Ajax calls. You should use this as the criteria for choosing whether to use GET or POST when processing forms on your web page as well.
TRACEBACK: http://javascript.about.com/od/ajax/a/ajaxgp.htm