Returns garbled characters for jquery Ajax requests.

Source: Internet
Author: User

Address: http://blog.csdn.net/hongweigg/article/details/6756278

 

1. jquery Ajax request. garbled characters are received in the background

You can explicitly set the contenttype encoding to UTF-8, and the background can be decoded normally, for example:

[JavaScript]
View plaincopyprint?
  1. Contenttype: "application/X-WWW-form-urlencoded; charset = UTF-8 ",
contentType: "application/x-www-form-urlencoded; charset=utf-8",

 

[JavaScript]
View plaincopyprint?
  1. Jquery (form). ajaxsubmit ({
  2. URL: "dologin? User = ",
  3. Type: "Post ",
  4. Datatype: "JSON ",
  5. Contenttype: "application/X-WWW-form-urlencoded; charset = UTF-8 ",
  6. Success: showloginresponse
  7. });
jQuery(form).ajaxSubmit({url: "doLogin?user=a",type: "post",dataType: "json",contentType: "application/x-www-form-urlencoded; charset=utf-8",success: showLoginResponse}); 

If you use escape () without adding contenttype: "application/X-WWW-form-urlencoded; charset = UTF-8 ",

The post body contains fewer bytes than the specified content length.

.

 

2. garbled characters returned by jquery Ajax

Have you found that no matter how you set the encoding, the data received by the client is always garbled? Please refer to the following example. Do you find any problems?

[Java]
View plaincopyprint?
  1. VoidSavetempkind (httpservletrequest req, httpservletresponse resp)
  2. ThrowsServletexception, ioexception {
  3. Printwriter PW = resp. getwriter ();
  4. Resp. setcontenttype ("text/text; charset = GBK ");
  5.  Try{
  6. // Do Process
  7. PW. Write ("successful ");
  8. }Catch(Exception e ){
  9. PW. Write ("failed ");
  10. }
  11. }
Void savetempkind (httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {printwriter PW = resp. getwriter (); resp. setcontenttype ("text/text; charset = GBK"); try {// do process PW. write ("succeeded");} catch (exception e) {PW. write ("failed ");}}

The problem is that resp. setcontenttype ("text/text; charset = GBK ");
The statement should be in

[JavaScript]
View plaincopyprint?
  1. Printwriter PW = resp. getwriter ();
PrintWriter pw = resp.getWriter();

Before, not after. As in the preceding example, no matter how you set the response encoding, the front-end garbled code remains the same. The possible cause is that the PW encoding is determined before obtaining the priintwriter. Therefore, the encoding declaration should be prior to obtaining the printwriter object.

 

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.