I'm bored today, so I want to solve the problems left over from the past, such as the headache-inducing Jquery garbled problem. In fact, there are already many articles in this area, but few of them comprehensively solve various problems. I will summarize them today for my own convenience and for everyone’s convenience.
The reason is very simple: In fact, his Chinese garbled characters are because the contentType does not specify the encoding. There are different settings for this place in different Jquery versions. Take what I encountered, jquery- 1.6.1 and jquery-1.8.3 have different definitions.
Solution: In the jquery-1.6.1 file, search for 'contentType' and then add; charset=UTF-8 after application/x-www-form-urlencoded and finally become Change it to contentType: "application/x-www-form-urlencoded; charset=UTF-8".
In this way, the problem of garbled characters after submitting through the post method can be perfectly solved.
If there are still garbled characters, it can only be said that there is a problem with the encoding of the page you receive. This is because the asynchronous object XMLHttpRequest decodes the UTF-8 encoding when processing the returned responseText. Therefore, if you use the post method, you must save this page and change the encoding of the page file to UTF-8 (please remember).
Before modification, the entire FROM form data was all garbled. For this situation, you can easily solve it by following the above method.
The following is a summary of some special situations, sourced from the Internet:
Solution 2 for Chinese garbled characters in JQuery Ajax submission
I have never found it when using Jquery before, but it will happen when submitting with Ajax Garbled characters appear, I guess it may be due to encoding
There may be the following reasons:
1. The encoding of HTML is not uniform: For example, the page uses GB2312, and it seems that JQuery It's not very well supported. I have always used UTF-8 before, but I have never found it;
2. The encoding of the file, this is not easy to see on the surface. The default text format file saved by the Simplified Chinese version of the operating system is GB2312. It is recommended that the simplest solution is to change the file into UTF-8 format
. Use JS escape to process the submitted Chinese text, so that garbled characters will not appear.
For example: