In jQuery, the Ajax Post submits a Chinese garbled solution), jqueryajax

Source: Internet
Author: User

Solution for submitting Chinese garbled characters in Ajax Post in jQuery (conversion), jqueryajax

Introduction: In the Ajax POST request of jQuery, the request is made. The Chinese characters in the request are displayed as garbled characters in the background. How can this problem be solved?

 

Problem introduction:

Var regid = $ ('# oregion'). combobox ('getvalue ');

// Var sname = $ ('# sname'). val ();

Var sname = encodeURI ($ ('# sname'). val (), "UTF-8 ");

If (regid! = "" & Regid! = 'Undefined '){

$. Ajax ({

Async: true,

Type: "POST ",

DataType: "json ",

Url: "<% = request. getContextPath () %>/secretary/isHaSUSEr. do? Regid = "+ regid +" & sname = "+ sname,

Success: function (data ){

If (data. mes! = "" & Data. mes! = Null ){

$. Messager. alert ('warning', data. mes );

Return false;

}

}

});

}

}

The above code can pass Chinese characters normally in FF/Chrome, but there is a problem in IE, and the output information in the background is garbled.

 

Solution:

1. Set contentType: "text/plain; charset = UTF-8". the problem persists.

2. Check again the jsp page where the ajax code is located, all of which use UTF-8, but the problem still exists.

3. in the back-end java code, the UTF-8, gbk, and gb2312 encoding transformations are forcibly used. The problem still exists after analysis, and it is determined that it may be related to content transmission in the browser or JQuery, at present, it cannot be determined accurately.

 

Solution:Front end: encodeURI ($ (dom). val (), "UTF-8"); encode with encodeURI

Back-end Java code:

URLDecoder. decode (inputStr, "UTF-8"): the backend code decodes the string passed in the previous section.

 

Summary:Finally, you can view the string information transmitted by the Ajax Post request to display Chinese characters normally.


JQuery ajax Chinese garbled

1. The front and back-end encoding should be unified;
2. When using gb2312 encoding, do not use $. get () or $. post () of jquery for ajax submission, because the two methods are UTF-8 by default;
3. Use $. add contentType: "pplication/x-www-form-urlencoded; charset = GB2312" to ajax () and write it in the following format to avoid garbled code in most cases:
$. Ajax ({
Type: "POST ",
ContentType: "pplication/x-www-form-urlencoded; charset = GB2312 ",
Url: "XXX",
Data :{},
Success: function (msg ){
Alert (msg );
}
});
 
Jquery post garbled submission to struts2 Chinese Garbled text many methods have been tried using UTF-8

Hello, this problem I encountered, if you are sure that your Struts side and JSP side are set to a unified UTF-8, then you do so.
The following solutions are separated and compared according to your configuration.
1 struts2 Configuration

In struts. add struts to the properties file. i18n. encoding = UTF-8 or in struts. add <constant name = "struts. i18n. encoding "value =" UTF-8 "/>

2 Databases

2.1 The DDL format of the mysql table should be ENGINE = InnoDB default charset = utf8; you can use show create table tbname to view the data.

2.2 When installing the mysql database, you should use utf8 encoding to install it by default. You can view the default encoding method of the mysql database in the directory my. the default-character-set Value of mysql and mysqld In the INI file is utf8 or "\ s;" in the mysql database command line.

3 Page

Add <% @ page pageEncoding = "UTF-8" contentType = "text/html; charset = UTF-8" %> on each page, right-click the jsp file on the jsp page, this is important if the encoding method is UTF-8.

4. jdbc connection

Set characterencoding to a UTF-8 such as jdbc. mysql. url = jdbc: mysql: // localhost: 3306/db? UseUnicode = true & characterEncoding = UTF8

5 tomcat

To ensure that the get/post Data adopts the same UTF-8 encoding. the following settings are made in xml: <Connector port = "8080" protocol = "HTTP/1.1" connectionTimeout = "20000" redirectPort = "8443" URIEncoding = "UTF-8"/>
OK. Done.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.