Differences between ajax request post and get and the selection of get post

Source: Internet
Author: User

The simplest difference:

1. When a Get request is used, the parameter is displayed in the URL, but the Post method is not displayed.

2. A small amount of data is sent using the Get request, and a large amount of data is sent using the Post request.

3. Pay attention to cache issues for get requests. post requests do not need to worry about this issue.

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.

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 ";
That is, the get parameters must be spliced into the url.

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"];

Post parameters do not need to be spliced into URLs.

Request. QueryString ["strName"] For the get Method
The post method uses Request. Form ["strName"] to receive

Note:
Although the two submission methods can use Request ("strName") to obtain the submitted data, this affects the program efficiency and is not recommended.
In general, try to avoid using the Get method to submit the form, because it may cause security problems.

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.

When to use Get requests and Post requests

The purpose of a Get request is to give the server some parameters to obtain the list from the server. For example: list. aspx? Page = 1, indicating to get the data on the first page

The purpose of the Post request is to send some parameters to the server, such as the content in form.

The following example shows the difference between a Get request and a Post request when sending the same data segment.

GET is simpler and faster than POST, and can be used in most cases.

However, use POST requests in the following cases:

Unable to use cached files (updating files or databases on the server)
Send large amounts of data to the server (there is no limit on the size of POST data)
When sending user input containing unknown characters, POST is more stable and reliable than GET.

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.