How to transmit garbled Chinese parameters in ajax

Source: Internet
Author: User

Ajax how to pass Chinese parameters garbled solution for some reason ajax by default is the uft-8 encoding, so we use a gbk is prone to garbled problem, the following describes how to solve php ajax garbled code.

How to transmit garbled Chinese parameters in ajax
For some reason, ajax is the encoding of the uft-8 by default, so we use a gbk will be prone to garbled problem, we will tell you the php tutorial ajax garbled solution.
Pass Chinese parameters, and then modify the database tutorial.

<Script>
Var url = "admin/ajaxmodify. php? "+ Key +" = ";
Url + = encodeuricomponent (value ));
Xmlhttp. open ("get", url, true );
Xmlhttp. send (null );
</Script>
The parameter is Chinese. encodeuricomponent. This method must be called twice.
*/

Function utf8rawurldecode ($ source ){
$ Decodedstr = "";
$ Pos = 0;
$ Len = strlen ($ source );
While ($ pos <$ len ){
$ Charat = substr ($ source, $ pos, 1 );
If ($ charat = '% '){
$ Pos ++;
$ Charat = substr ($ source, $ pos, 1 );
If ($ charat = 'U '){
// We got a unicode character
$ Pos ++;
$ Unicodehexval = substr ($ source, $ pos, 4 );
$ Unicode = hexdec ($ unicodehexval );
$ Entity = "& #". $ unicode .';';
$ Decodedstr. = utf8_encode ($ entity );
$ Pos + = 4;
}
Else {
// We have an escaped ascii character
$ Hexval = substr ($ source, $ pos, 2 );
$ Decodedstr. = chr (hexdec ($ hexval ));
$ Pos + = 2;
}
} Else {
$ Decodedstr. = $ charat;
$ Pos ++;
}
}
Return $ decodedstr;
}


/*
Note: In js, encodeuricomponent () or encodeuri () is recommended for character transcoding, rather than escape (). The reason is that escape () only converts ascii characters to % unnnn. If you want to use more characters, such as the UTF-8 character library, you must use encodeuricomponent () or encodeuri () can be converted to % nn.

Js: encodeuricomponent -- decodeuricomponent; php: rawurlencode -- rawurldecode


*/

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.