Processing of passing Chinese parameters in js ajax

Source: Internet
Author: User

Garbled characters may occur when js ajax transmits Chinese characters. Because of the encoding problem, ajax is uft8 by default. In this way, url garbled characters are available. We just need to simply use the js encodeURI function for conversion.

The processing code for passing Chinese parameters in ajax:

The Code is as follows: Copy code

Js:

Send_request ('HTTP: // www. bKjia. c0m/mini_do.php? Username = '+ encodeURI (username) +' & phone = '+ encodeURI (phone) +' & content = '+ encodeURI (content ));

In mini_do.php:

$ Username = urldecode ($ _ REQUEST [username]);
$ Phone = urldecode ($ _ REQUEST [phone]);
$ Content = urldecode ($ _ REQUEST [content]);

Finally, some friends said they could use the escape function.

Var htmer = "getcode =" + escape (getcode) + "& Content =" + escape (Content );

I do not recommend this because it has a higher error. Some Chinese characters cannot be recognized. Another function is encodeURIComponent, which is comprehensive.

EncodeURIComponent converts special characters such as Chinese and Korean to url encoding in UTF-8 format, therefore, if you want to use encodeURIComponent to transmit parameters to the backend, you must use backend decoding to support UTF-8 (the encoding method in form is the same as that on the current page)

Summary

From the above

Escape unencoded characters are 69: *, +,-,.,/, @, _, 0-9, a-z, A-Z

EncodeURI is not encoded with 82 characters :!, #, $, &, ', (,), *, +,-,.,/,:,;, = ,?, @,_,~, 0-9, a-z, A-Z

EncodeURIComponent has 71 unencoded characters :!, ',(,),*,-,.,_,~, 0-9, a-z, A-Z

From the encoding range, we chose encodeURI as the best choice.

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.