Tuesday, March 3, 2020
When to Use GET and POST in Ajax
When to Use GET and POST in Ajax When you use Ajax (Asynchronous JavaScript and XML) to access the server without reloading the web page, you have two choices on how to pass the information for the request to the server: GET or POST. These are the same two options that you have when passing requests to the server to load a new page, but with two differences. The first is that you are only requesting a small piece of information instead of an entire web page. The second and most noticeable difference is that since the Ajax request doesnt appear in the address bar, your visitors wont notice a difference when the request is made. Calls made using GET will not expose the fields and their values anywhere that using POST does not also expose when the call is made from Ajax. What You Should Not Do So, how should we make the choice as to which of these two alternatives should be used? A mistake 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 noticeable difference between GET and POST calls in Ajax is that GET calls still have the same limit on the amount of data that can be passed as when requesting a new page load. The only difference is that because youre only processing a small amount of data with an Ajax request (or at least thats how you should use it), you are far less likely to run into this length limit from within Ajax like you would with loading a complete web page. 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, you would probably be 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 isnt a good reason for choosing between GET and POST, then what should we use to decide? These two methods were in fact set up for entirely different purposes, and the differences between how they work are 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 really anywhere these methods might be employed. The Purpose of GET and POST GET is used as the name implies: to get information. its intended to be used when you are reading information. Browsers will cache the result from a GET request and if the same GET request is made again, they will display the cached result rather than re-running the entire request. This is not a flaw in the browser processing; its deliberately designed to work that way so as to make GET calls more efficient. A GET call is just retrieving the information; its not meant to change any information on the server, which is why requesting the data again should return the same results. The POST method is for posting or updating information on the server. This type of call is expected to change the data, which is why the results returned from two identical POST calls may very well be completely different from one another. The initial values before the second POST call will be different from the values before the first because the initial call will have updated at least some of those values. A POST call will therefore always obtain the response from the server rather than keep a cached copy of the prior response. How to Choose GET or POST Instead of choosing between GET and POST based on the amount of data you are passing in your Ajax call, you should choose 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, 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. Use POST if your call is going to write any data at all to the server. In fact, you should not only use this criterion for selecting between GET and POST for your Ajax calls but also for when selecting which should be used for processing forms on your web page.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.