I haven't studied ajax very carefully before. I just used it when I used it. I found problems and then found solutions. The following is a small summary of my process of finding solutions to problems.
1. Talking about the difference between Ajax Get and Post
Get method:
Using the get method to transmit simple data, but the size is generally limited to 1KB, the data is appended to the url Send in (HTTP header transmission), that is to say, the browser appends each form field element and its data to the end of the resource path in the request line in the format of URL parameters. The most important thing is that it will be cached by the client's browser, so others can read the customer's data, such as account number and password, etc. from the browser's history. Therefore, in some cases, the get method can cause serious security issues.
Post method:
When using the POST method, 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 parameters of the URL address. Transmission, the amount of data transmitted using POST method is much larger than that using GET method.
In short, the GET method transmits a small amount of data, high processing efficiency, low security, and will be cached, while the opposite is true for POST.
When using the get method, please note: :
1 For get requests (or any involving url passed parameters), the passed parameters must first be processed by the encodeURIComponent method. Example: var url = "update.php?username=" encodeURIComponent(username) "&content=" encodeURIComponent
(content) "&id=1" ;
Be careful when using the Post method:
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 entity. Usually SetRequestHeader("Context-Type", "application/x-www-form-urlencoded" of the XmlHttpRequest object is used ;"). Example:
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
2. The parameters are key-value pairs with one-to-one name/value correspondence. Each pair of values uses Separated by ampersand. For example, var name=abc&sex=man&age=18, please note that var name=update.php?
abc&sex=man&age=18 and var name=?abc&sex=man&age=18 are both wrong;
3. Parameters are sent in the Send (parameter) method, for example: xmlHttp.send(name); If it is the get method, directly xmlHttp.send(null);
4. Server-side request parameters distinguish Get and Post. If it is the get method, then $username = $_GET["username"]; If it is the post method, then $username = $_POST["username"];
The differences between the Post and Get methods are as follows:
1. When Post transmits data, it does not need to be displayed in the URL, but the Get method must be displayed in the URL.
2.Post transmits a large amount of data, which can reach 2M, while the Get method can only transfer about 1024 bytes due to the URL length limit.
3.Post, as the name suggests, is to transmit data to the server segment , Get is to obtain data from the server segment. The reason why Get can also transmit data is only designed to tell the server what kind of data you need. Post information is used as the content of the http request, and Get is in the Http header. transmitted.
The get method uses Request.QueryString["strName"] to receive
The post method uses Request.Form["strName"] to receive
Note:
Although the two submission methods can be unified, Request("strName ") to obtain submitted data, but this has an impact on program efficiency and is not recommended.
Generally speaking, try to avoid using the Get method to submit a form, because it may cause security problems
AJAX garbled code problem
Causes of garbled code:
1. The default character encoding of the data returned by xtmlhttp is utf-8. If the client page is gb2312 or other encoded data, garbled characters will be generated
2. The default character encoding of the data submitted by the post method is utf-8. If the server is gb2312 Or other encoded data will produce garbled characters
The solutions are:
1. If the client is gb2312 encoding, specify the output stream encoding on the server
2. Server side Both the client and the 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 done the above method and still return garbled characters, check whether your method is get. For get requests (or anything involving url passing parameters), the passed The parameters must be processed by the encodeURIComponent method first. If they are not processed by encodeURIComponent, garbled characters will also be produced.
Below is an example I found. Because it is well written, I posted it here. The one I wrote is relatively simple, and it is not It’s very standard, so it’s better to refer to what others have written, haha!