Passing utf-8 strings between php and javascript

半城伤御伤魂 提交于 2020-02-24 06:17:25

问题


I'm having problems passing utf-8 strings to javascript (ajax). Currently i'm using rawurlencode on the PHP side and unescape on the javascript side.

The problem is in latin and rawurlencode doesn't support it fully.

Is there any alternative or any better option?


回答1:


The solution was in json_encode functions. The problems stopped when i added JSON_HEX_APOS|JSON_HEX_QUOT.

Thanks!




回答2:


use json_encode in PHP and receive responses as JSON (jQuery is helpful) ajax is sent in utf-8 by default, so You just have to return utf-8

php's utf8_encode(data) gets an ISO-8859-1 string as the data argument.

need more suggestions? Tell me where You get the text from ;)




回答3:


From experience, Javascript's escape() (ant thus unescape()) are not Unicode (UTF-8) friendly. Use encodeURIComponent() and decodeURIComponent() instead.

Anyway, as the docs says:

The escape() function should not be used to encode URIs.




回答4:


If php is doing the encoding and js decoding whay not simply not encode in php and encode in js as well? Not really an answer so much as a work around i guess.



来源:https://stackoverflow.com/questions/2268804/passing-utf-8-strings-between-php-and-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!