Differences between Jquery ajax post and GET

Source: Internet
Author: User

1: GET access to the browser is considered to be idempotent
It means that the same URL has only one result. [the same means that the whole URL string is exactly matched]
Therefore, if the URL string remains unchanged during the second visit, the browser directly obtains the result of the first visit.

POST is regarded as a variable access (the browser believes that the submission of POST must be changed)

To prevent idempotent GET access, add? + New Date ();, [in short, the URL string for each access is different]

This principle should also be followed when designing web pages

2: I. Differences between Ajax Get and Post

Get method:
The get method can be used to transmit simple data, but the size is generally limited to 1 kb, and the data is appended to the url for sending (http header transfer), that is, the browser attaches the form field elements and their data to the resource path in the request line according to the URL parameter format. The most important thing is that it will be cached by the client's browser, so that others can read the customer's data from the browser's historical records, for example, account and password. Therefore, in some cases, the get method may cause serious security problems.

Post method:
When the POST method is used, the browser sends the form field elements and their data to the Web server as the entity content of the HTTP message, rather than as the URL address parameter, the amount of data transmitted in POST mode is much larger than that transmitted in GET mode.

In short, the GET method delivers a small amount of data, high processing efficiency, low security, and will be cached, whereas the POST method does not.

Note the following when using get:
1 For get requests (or any request involving url-based parameters), The passed parameters must be processed by the encodeURIComponent method. For example: var url = "update. php? Username = "+ encodeURIComponent (username) +" & content = "+ encodeURIComponent

(Content) + "& id = 1 ";


Note the following when using Post:
1. set the Context-Type of the header to application/x-www-form-urlencode to ensure that the server knows that there are parameter variables in the object. the SetRequestHeader ("Context-Type", "application/x-www-form-urlencoded;") of the XmlHttpRequest object is usually used ;"). Example:

XmlHttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
2. the parameter is a key-value pair with one-to-one correspondence between names and values. Each pair of values is separated by an ampersand. for example, var name = abc & sex = man & age = 18. Note that var name = update. php?

Abc & sex = man & age = 18 and var name =? Abc & sex = man & age = 18 is incorrect;
3. the parameter is sent in the Send (parameter) method, for example, xmlHttp. send (name); if it is in get mode, xmlHttp. send (null );

4. Server Request Parameters are differentiated between Get and Post. For the get method, $ username = $ _ GET ["username"]; for the post method, $ username = $ _ POST ["username"];

AJAX garbled

Cause of garbled characters:
1. The default character encoding of data returned by xtmlhttp is UTF-8. If the client page is gb2312 or other encoding data, garbled characters are generated.
2. The default character encoding for data submitted by the post method is UTF-8. If the server side is gb2312 or other encoding data, garbled characters are generated.

Solutions:
1. If the client is gb2312 encoded, specify the output stream encoding on the server.
2. Both the server and client use UTF-8 encoding.

Gb2312: header ('content-Type: text/html; charset = GB2312 ');

Utf8: header ('content-Type: text/html; charset = UTF-8 ');

Note:If you have already followed the above method and returned garbled characters, check whether your method is get. For get requests (or any request involving url parameters ), the passed parameters must be processed by the encodeURIComponent method. if you do not use encodeURIComponent for processing, garbled characters are generated.

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.