Jquery AJAX Post and get differences

Source: Internet
Author: User
1:get access to the browser is considered to be idempotent
Just a single result of the same URL [same as the entire URL string exactly matches]
So when the second visit if the URL string does not change the browser is directly out of the results of the first visit

Post is considered to be a variable access (the browser thinks that post commits must be changed)

Prevent idempotent access to get add after URL? +new Date ();, [in short, the URL string for each access is different]

You should also follow this principle when designing Web pages.

2: one. Talk about the difference between the get and post of Ajax

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

Post mode:
When a post is used, the browser sends each form field element and its 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 sent using the Get method.

In short, the Get method transmits small amounts of data, high processing efficiency, low security, will be cached, and post instead.

Use the Get method to be aware of:
1 for a GET request (or any that involves a URL pass parameter), the passed arguments are processed first by the encodeURIComponent method. Example: var url = "Update.php?username=" +encodeuricomponent ( username) + "&content=" +encodeuricomponent

(content) + "&id=1";


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

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

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

4. The server-side request parameter distinguishes between get and post. If it is GET mode then $username = $_get["username"]; If it is POST mode, then $username = $_post["username"];

Ajax garbled problem

Causes of garbled:
1, xtmlhttp the data returned by default character encoding is Utf-8, if the client page is gb2312 or other encoded data will produce garbled
2, Post method submission data The default character encoding is Utf-8, if the server side is gb2312 or other encoded data will produce garbled
The solutions are:
1, if the client is GB2312 encoding, the server specifies the output stream encoding
2, the server side and the 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 done the above method, or return garbled words, check whether your way is get, for the GET request (or all involved in the URL to pass parameters), the passed parameters must be preceded by the encodeURIComponent method processing. If not treated with encodeuricomponent, it will also produce garbled characters.
  • 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.