Home > Web Front-end > JS Tutorial > jquery garbled code and contentType attribute setting problem solution_jquery

jquery garbled code and contentType attribute setting problem solution_jquery

WBOY
Release: 2016-05-16 17:44:23
Original
993 people have browsed it

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).
jquery garbled code and contentType attribute setting problem solution_jquery
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:

Copy code The code is as follows:

//Save data
$.ajax({
url:"/guide/savecomments.aspx" ,
type:"POST" ,
dataType:"json" ,
data:"Action=SaveComment&CommNickName=" escape( CommNickName.val()) "&CommContent=" escape(CommContent.val()) "&GuideID=" GuideID.val() "&ScoreLogType=<%=ScoreLogType %>",
success:function(results){
alert(results.message);

---------------------------------- ---------------------------------------------
The solution is to use the encoding function encodeURIComponent(string) of js to encode the Chinese "Wang Xiaoming" into "Wang Xiaoming", and it will be OK
By the way, URIEncoding=UTF-8 under my tomcat
------------------------------------------------ ----------------------------------
Today when using jquery to detect user names, for English and numeric users The name detection is correct, but when it comes to Chinese, the detection is wrong. After searching online for a period of time, I finally found the reason. It is a garbled code problem. The solution:
1. As long as there is data submission in ajax, if the page encoding is not utf-8, the submitted data should be encoded. The encoding function of js is escape()
2. Decode the data after receiving it on the server page, and then perform relevant processing on the data before encoding
3. Decode after returning to the client
4. If the data is not submitted, but the data is obtained directly from the server, just set Response.Charset="gb2312" directly on the server page without encoding and decoding
vbscript corresponds to the escape() and unescape() function program codes in js
----------------------------- --------------------------------------------------
It is solved by the following processing method:
Encode the parameter priceName="encodeURI(priceName)" when passing the parameter, or you can use encodeURIComponent();
No need to do this on the server side Other processing: String priceName = request.getParameter("priceName");
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