Home > Web Front-end > JS Tutorial > body text

What problems will you encounter when using get and post in Ajax and how to deal with them

php中世界最好的语言
Release: 2018-04-04 11:31:25
Original
1680 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!