The difference between get and post in Ajax and the method of Ajax garbled solution

Source: Internet
Author: User

A. Talking about the difference between get and post of Ajax

Individual form field elements and their data are appended 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.

parameter, the amount of data that is passed by post is much larger than the amount of data that is transmitted using the GET method.

In short, get-mode transfer data is small, high processing efficiency, low security, will be cached, and post instead.

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


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. such as var >

Abc&sex=man&age=18 and var >3. Parameters are sent in the Send (parameter) method, for 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"];

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.

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.