Ajax requests the difference between post and get and the selection of Get post _ajax related

Source: Internet
Author: User

The simplest difference:

1. When using a GET request, the parameter is displayed in the URL and the Post method is not displayed

2. Use GET request to send the data quantity is small, the POST request sends the data quantity to be big

3.get request attention to cache problem, POST request do not need to worry about this problem

Get Way:

You can transfer simple data in Get mode, but the size is typically limited to 1KB, the data is appended to the URL (HTTP header transfer), that is, the browser appends individual form field elements and their data to the resource path in the request line in the format of the URL parameter. The most important point is that it is cached by the client's browser, so that other people can read the customer's data, such as account number and password, from the browser's history. Therefore, in some cases, the Get method poses a serious security problem.

Post method:

When using post, the browser sends individual form field elements and their data to the Web server as the entity content of the HTTP message, rather than as a parameter to the URL address, and the amount of data that is passed by post is much larger than the amount of data that is transmitted using the GET method.

You need to be aware of using get methods:

1 for Get requests (or any that involve passing parameters to the URL), the passed parameters are processed first by the encodeURIComponent method. Example: var url = "Update.php?username=" +encodeuricomponent ( username) + "&content=" +encodeuricomponent

(content) + "&id=1";
That is, the pass parameter of get must be spliced into the URL

Use POST method to note:

1. Set header Context-type to Application/x-www-form-urlencode ensure that the server knows that there are parameter variables in the entity. The setrequestheader of the XMLHttpRequest object is usually used ( "Context-type", "application/x-www-form-urlencoded"). Cases:

Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
2. The parameter is a name/value one by one corresponding to the key value pairs, each pair of values separated by the & number. like Var name=abc&sex=man&age=18, pay attention to Var name=update.php?

The abc&sex=man&age=18 and the Var name=?abc&sex=man&age=18 are all wrong;
3. Parameters sent in the Send (parameter) method, example: Xmlhttp.send (name); If it is a get way, direct xmlhttp.send (NULL);

4. Server-side request parameters differentiate get and post. If the Get method is $username = $_get["username"]; If it is the POST method, then $username = $_post["username"];

Post's pass parameter does not need to be spliced into the URL

Get method receives with request.querystring["StrName"]
Post method receives with request.form["StrName"]

Although both submissions can be unified using request ("StrName") to obtain submission data, this has an impact on program efficiency and is not recommended.
In general, try to avoid submitting forms using get methods, as it can lead to security issues

Ajax garbled problem

The reason for the garbled:

1, Xtmlhttp returned the data default character encoding is Utf-8, if the client page is gb2312 or other encoded data will produce garbled
2, Post method submission data default character encoding is utf-8, if server-side is gb2312 or other encoded data will produce garbled

Solutions include:

1, if the client is gb2312 code, then specify the output stream code on the server
2, server-side and client are using 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 done so by the above method, or if you return garbled words, check if your method is get, for GET requests (or any of the URLs that involve passing parameters), The parameters that are passed are processed first by the encodeURIComponent method. If not treated with encodeuricomponent, it will produce garbled characters.

When to use a GET request and when to use a POST request

The purpose of a GET request is to give the server some parameters to fetch the list from the server. For example: list.aspx?page=1, which means getting the first page of data

The purpose of a POST request is to send some parameters to the server, such as the contents of a form.

The following examples are used to represent the difference between a GET request and a POST request when sending the same piece of data.

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

However, use POST requests in the following situations:

Unable to use cached files (update files or databases on the server)
to send large amounts of data to the server (post no data limit)
Post is more stable and reliable than get when sending user input containing unknown characters

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.