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.