简体   繁体   English

在php和javascript之间传递utf-8字符串

[英]Passing utf-8 strings between php and javascript

I'm having problems passing utf-8 strings to javascript (ajax). 我在将utf-8字符串传递给javascript(ajax)时遇到问题。 Currently i'm using rawurlencode on the PHP side and unescape on the javascript side. 目前我在PHP端使用rawurlencode ,在javascript端使用unescape

The problem is in latin and rawurlencode doesn't support it fully. 问题在于拉丁语和rawurlencode不完全支持它。

Is there any alternative or any better option? 有没有其他选择或更好的选择?

The solution was in json_encode functions. 解决方案是在json_encode函数中。 The problems stopped when i added JSON_HEX_APOS|JSON_HEX_QUOT . 当我添加JSON_HEX_APOS|JSON_HEX_QUOT时,问题就停止了。

Thanks! 谢谢!

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中使用json_encode并以JSON接收响应(jQuery很有用)默认情况下,ajax在utf-8中发送,所以你只需要返回utf-8

php's utf8_encode(data) gets an ISO-8859-1 string as the data argument. php的utf8_encode(data)获取ISO-8859-1字符串作为数据参数。

need more suggestions? 需要更多建议? Tell me where You get the text from ;) 告诉我你从哪里得到文字;)

From experience, Javascript's escape() (ant thus unescape() ) are not Unicode (UTF-8) friendly. 根据经验,Javascript的escape() (因此unescape()不是 Unicode(UTF-8)友好的。 Use encodeURIComponent() and decodeURIComponent() instead. 请改用encodeURIComponent()decodeURIComponent()

Anyway, as the docs says: 无论如何,正如文档所说:

The escape() function should not be used to encode URIs. escape()函数不应用于编码URI。

If php is doing the encoding and js decoding whay not simply not encode in php and encode in js as well? 如果php正在进行编码和js解码whay不仅不是在php中编码而且在js中编码也是如此? Not really an answer so much as a work around i guess. 我的猜测并不是一个真正的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM