Home >Web Front-end >JS Tutorial >What problems will you encounter when using get and post in Ajax and how to deal with them
This time I will bring you what problems you will encounter when using get and post in Ajax and how to deal with them. What are the precautions when using get and post in Ajax. The following is a practical case. Get up and take a look.
Problems encountered when using get:
1. Problem 1. Caching: When the URL is the same each time it is accessed, the client reads it directly Get the content in the local cache, even if the background data changes, the foreground will not change;
Solution: In? Then link a num=[random number Math.random()] or num=[Timestampnew Date().getTime()], '1.php?username="May"&'+num (There is no variable name here to avoid conflict with background parameters)
2. Problem 2. Garbled characters. When Chinese or special characters are passed, garbled characters appear in the front-end display.
Solution: Use encoding encodeURI('snail')
Test result: By using Chrome test, the two problems of get result have been solved
Problems encountered when using post:
Post method, data is passed in send() as a parameter;
Problem: When no request is set When the header is requested, the background does not process the data directly; the front desk cannot obtain the data returned by the background.
Solution: Manually set the request header (xhr.setRequestHeader('content-type','application/x-www- form-urlencoded')), declare the data type sent
[Note] There is no caching problem in the post, and there is no encoding problem (because it has been declared in the request header)
Test results: Tested using Chrome, the result is that the post does not declare header information, and the problem exists
Simply put: Get request and Post request The difference
1. When using the Get request, the parameters are displayed in the URL, but when using the Post method, they will not be displayed.
2. Use the Get request to send data The amount is small, but the Post request sends a large amount of data
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
How does ajax implement the encapsulation of network requests
How does Ajax implement the data loading list without disconnecting when clicking
The above is the detailed content of What problems will you encounter when using get and post in Ajax and how to deal with them. For more information, please follow other related articles on the PHP Chinese website!