Home > Web Front-end > JS Tutorial > How to use get and post in Ajax

How to use get and post in Ajax

php中世界最好的语言
Release: 2018-04-04 11:28:23
Original
1007 people have browsed it

This time I will show you how to use get and post in Ajax. What are the precautions when using get and post in Ajax. The following is a practical case, let's take a look.

Problems encountered when using get:

1. Problem 1. Cache: When the URL visited is the same each time, the client directly reads the contents of the local cache , even if the background data changes, the front desk 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 the request header is not set, the background directly The data is not processed; 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 or encoding problem in the post (because it has been declared in the request header)

Test result: Passed Use Chrome to test, and the result is that the post does not declare header information. The problem exists

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:

Ajax restful interface method of transmitting Json data

How to implement native AJAX encapsulation

The above is the detailed content of How to use get and post in Ajax. 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