Home  >  Article  >  Web Front-end  >  Questions about the use of get and post in Ajax

Questions about the use of get and post in Ajax

韦小宝
韦小宝Original
2018-01-09 09:29:591491browse

The editor below will bring you an article about the use of get and post in Ajax. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. If you are interested in ajax, please follow the editor to read the articles about the use of get and post in Ajax

Problems encountered when using get:

1. Question 1. Cache: When the url visited each time is the same, the client directly reads the content in the local cache. Even if the background data changes, the frontend will not change;

Solution: exist? 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.

The above is the entire content of the use of get and post in Ajax brought to you by the editor. I hope everyone will support PHP. Chinese website~

Related recommendations:

The ajax get request obtained a json format data, how to traverse it in js

The problem of inexplicable loss of ajax GET request parameters

Example to explain the Ajax post request jump page

The above is the detailed content of Questions about the use of get and post in Ajax. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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