最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin7浏览0评论

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:

$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):

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

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

Ränisipelgasöösel

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

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 mand 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.

UTF-8 is used on both client and server.

Any ideas?

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:

$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):

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

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

Ränisipelgasöösel

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

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 mand 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.

UTF-8 is used on both client and server.

Any ideas?

Share Improve this question asked Apr 13, 2012 at 13:46 kingmaplekingmaple 4,3205 gold badges35 silver badges44 bronze badges 7
  • I think you look for a method how to pare two json strings with each other. – hakre Commented Apr 13, 2012 at 13:50
  • No. This already works, I am paring them through hashing. Just that if the characters are different then hashing is also different. I need to be able to convert string to the same one that PHP json_encode() has it after encoding. It's only PHP json_encode() that does this, JSON.stringify(string)!=json_encode($string) – kingmaple Commented Apr 13, 2012 at 13:52
  • Have you tried with json_encode from the PHPJs project? phpjs/functions/json_encode:457 – hakre Commented Apr 13, 2012 at 13:54
  • Are you not sending the JSON string for Javascript to PHP or vice versa? In which case, can't you just use the string as received for the hashing, before decoding it? – DaveRandom Commented Apr 13, 2012 at 14:00
  • @Saxoier — Really? I looked up codepoint 00f6 – Quentin Commented Apr 13, 2012 at 14:12
 |  Show 2 more ments

4 Answers 4

Reset to default 3

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

rawurlencode( json_encode( $data ) );

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

JSON.parse( decodeURIComponent( data ) );

Hope this helps.

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.

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!

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

is you html page encoding utf-8?

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

发布评论

评论列表(0)

  1. 暂无评论