Ajax, choose Get or post?

Source: Internet
Author: User
Tags current time

Transferred from: http://developer.51cto.com/art/200906/130400.htm

When we use AJAX to access the server without reloading the page, there are two choices to transfer the request information to the server. These two options are get and post respectively.

These two choices have two differences when sending request information to the server to load a new page. The first difference is that you are requesting a small part of the information rather than the entire page. The second difference is also the most obvious difference is that Ajax requests do not appear in the Address bar, so when sending requests, there is no difference on the visitor's screen. Using get generation calls does not expose the domain and their values, and using post is not exposed. So, how do we choose between the two?

One mistake a beginner might make is to use the GET command for most calls, just because the command is relatively easier to write. The most significant difference between get and post calls is that when a new page request is issued, the get call has the same limit for the same amount of data. The only difference is that you handle a small amount of Ajax request data, and you do not want to do this by restricting the length of the page to load. Beginners may use post in a few cases, such as when they do need to send more information.

When we have a large amount of data to be transmitted, the best way is to emit multiple Ajax calls that only deliver a small amount of information at a time. If you are sending large amounts of data with an Ajax call, it is best to end this practice, because doing so does not save time.

So is it necessary to transfer large amounts of data to justify our hesitation between get and post? Both methods are designed for different purposes, and the difference lies in the purpose of their use. This applies not only to the use of Get and post, but also to other methods.

Get is intended to be used to obtain information just as its name would be. It is designed to display the information you want to read on the page. The browser buffers The execution result of the GET request, and if the same GET request is sent again, the browser will display the buffered results instead of running the entire request again. This process differs from the browser's process, but it is intentionally designed to make get calls more efficient. The get call retrieves the data to be displayed on the page, and the data is not changed on the server, so the same result is obtained when the same data is re-requested.

The Post method should be used where you need to update the server information. If a call is to change the data that is saved on the server, the results returned from the two same post calls may be completely different because the value of the second post call is not the same as the first value because some of these values have been updated by the first call. A post call typically gets a response from the server rather than keeping the buffer of the previous response.

Therefore, do not use the amount of data to decide whether to choose get or post, but should be based on the purpose of making a choice between the two. Use Get if the call is to retrieve data on the server. If the value to be retrieved changes over time and the update process changes, add a current time parameter to the get call so that subsequent calls do not use the previous incorrect buffer. If the call is to send arbitrary data to the server, you can use post.

In fact, we should not just use this standard as a criterion for choosing how get and post calls are used, but it can also be used as a choice when dealing with the form of a page.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.