) Jquery code garbled

Source: Internet
Author: User

Jquery's powerful functions have become more and more popular among many friends. However, Ajax is a little too difficult to take care of the feelings of the Chinese people.

Data Transfer Encoding only supports UTF-8, although it is a globally unified code, but also consider the Chinese name local website encoding problem

In addition, on the sending end of jquery, no matter whether you have set the program-level encoding or page-level encoding, jquery uses UTF-8 encoding to pass through

Some websites are operating a long event, it is indeed a little difficult to completely modify the UTF-8, good complaints sent here, below is a solution example written by myself

Hope to help you.

Principle:

Use the escape method on the ajax sender. Then use the unescape Method for conversion at the receiving end

Example:

Gb2312.html

 

  1. <Html>
  2. <Head>
  3. <MetaHttp-equiv = "Content-Type"Content = "text/html;Charset = gb2312" />
  4. <SCRIPTLANGUAGE = "JavaScript"Src = "js/jquery-1.2.4.js"> </script>
  5.  <SCRIPTLanguage = "JavaScript">
  6.   <! --
  7.     $ (Document). Ready (function (){
  8.        $ ("# BTN"). Click (function (){
  9.          $. Get ("PHP/gb2312.php", function (data ){
  10.            
  11.                 $ ("# Content" ).html (unescape (data); // you can click unescape here.
  12.          
  13.          })
  14.          
  15.        });
  16.     });
  17.   //-->
  18.   </SCRIPT>
  19. <Title> untitled document </title>
  20. </Head>
  21.  
  22. <Body>
  23.  <DivID = "btn"> get ajax Chinese </div>
  24.  <DivID = "content"> </div>
  25.  
  26. </Body>
  27. </Html>

Gb2312.php

 

  1. <?
  2. EchoEscape ("GB2312 Chinese");
  3. FunctionEscape ($ Str){
  4.    Preg_match_all ("/[\ X80-\ xff]. | [\ x01-\ x7f] +/", $ str, $ r );
  5.    $ Ar=$ R [0];
  6.    Foreach ($ arAs$ K => $ v){
  7.      If (ord ($ v [0])<128)
  8.        $ Ar [$ k]=Rawurlencode ($ V );
  9.      Else
  10.        $ Ar [$ k]="% U". bin2hex (iconv ("GB2312", "UCS-2", $ v ));
  11.    }
  12.    ReturnJoin ("", $ Ar );
  13. }
  14. ?>

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.