如何为 JavaScript 和 JSON 正确编码 UTF-8?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10142050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:48:58  来源:igfitidea点击:

How to properly encode UTF-8 for JavaScript and JSON?

phpjavascriptjsonencodingutf-8

提问by kingmaple

I have a problem creating an input validation hash. JavaScript submits data to API and API validates the sent data with json_encode. Basically it works like this:

我在创建输入验证哈希时遇到问题。JavaScript 向 API 提交数据,API 使用 json_encode 验证发送的数据。基本上它是这样工作的:

$input=array('name'='John Doe','city'=>'New York');
$validationHash=sha1(json_encode($input).$key); // Key is known to both servers

If PHP connects to another server, then everything works. It also works through JavaScript (I have a custom sha1() function there as well):

如果 PHP 连接到另一台服务器,则一切正常。它也可以通过 JavaScript 工作(我也有一个自定义的 sha1() 函数):

var validationHash=sha1(JSON.stringify({'name':'John Doe','city'=>'New York'})+key);

var validationHash=sha1(JSON.stringify({'name':'John Doe','city'=>'New York'})+key);

My problem comes when the string contains UTF-8 characters. For example, if one of the values is:

当字符串包含 UTF-8 字符时,我的问题就出现了。例如,如果其中一个值是:

R?nisipelgas??sel

Then PHP server that receives the command converts it to this after JSON encoding:

然后接收命令的 PHP 服务器在 JSON 编码后将其转换为:

R\u00e4nisipelgas\u00f6\u00f6sel

I need to do this in JavaScript as well, but I haven't been able to work out how. I need to make sure that I send proper validation hash to the server or the command fails. I found from Google that unescape(encodeURIComponent(string)) and decodeURIComponent() could be used, but neither gives me the same string that PHP has and validates with.

我也需要在 JavaScript 中执行此操作,但我无法弄清楚如何执行。我需要确保我向服务器发送了正确的验证哈希,否则命令会失败。我从 Google 发现 unescape(encodeURIComponent(string)) 和 decodeURIComponent() 可以使用,但它们都没有给我与 PHP 具有和验证的相同的字符串。

UTF-8 is used on both client and server.

UTF-8 用于客户端和服务器。

Any ideas?

有任何想法吗?

采纳答案by kingmaple

It does not seem to be possible. The only working solution I have found is to encode all data with encodeURIComponent() on browser side and with rawurlencode() on PHP side and then calculate the JSON from these values in arrays.

这似乎是不可能的。我发现的唯一可行的解​​决方案是在浏览器端使用 encodeURIComponent() 对所有数据进行编码,在 PHP 端使用 rawurlencode() 对所有数据进行编码,然后从数组中的这些值计算 JSON。

回答by Daniel Barde

My fix was to raw url encode my json data like so.

我的解决方法是像这样对我的 json 数据进行原始 url 编码。

rawurlencode( json_encode( $data ) );

And then from within javascript decode the raw url encoded json and then parse the json string like so.

然后从 javascript 中解码原始 url 编码的 json,然后像这样解析 json 字符串。

JSON.parse( decodeURIComponent( data ) );

Hope this helps.

希望这可以帮助。

回答by A Person

Why not base64 encode the data for safe transport? It encodes UTF-8 characters in a safe string that can be sent across different mediums - php, javascript etc. That way you can base64 decode the string at the receiving end. Voila!

为什么不对数据进行 base64 编码以实现安全传输?它将 UTF-8 字符编码为一个安全的字符串,可以通过不同的媒介发送 - php、javascript 等。这样你就可以在接收端对字符串进行 base64 解码。瞧!

By base64 encoding the data, i mean base64 encoding the values and not the whole json string

通过base64编码数据,我的意思是base64编码值而不是整个json字符串

回答by Roozbeh Sharafi

is you html page encoding utf-8?

你是 html 页面编码 utf-8 吗?

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">