简体   繁体   English

如何在Java脚本中编码非ASCII字符?

[英]how to encode non-ascii characters in java script?

Suppose ch = á expected output is = \á but current output = %E1 when I use escape(ch) and current output = %C3%A1 when I use encodeURIComponent(ch) 假设ch =á预期输出为= \\ u00e1,但是当我使用escape(ch)时当前输出=%E1,而当我使用encodeURIComponent(ch)时当前输出=%C3%A1

I am using API which accept unicode characters. 我正在使用接受Unicode字符的API。

try utf8 encoding for the characters. 尝试对字符进行utf8编码。 Here is code: 这是代码:

https://code.google.com/p/archfirst/source/browse/trunk/html/apps/bfclient-jquery-backbone/js/libs/utf8_encode.js?r=1029

ch = á expected output is = \á ch =á预期输出为= \\ u00e1

Do never use escape . 不要从不使用escape Do use encodeURIComponent for x-www-form-urlencode ing forms for POST or GET HTTP request. 对于POST或GET HTTP请求,请对x-www-form-urlencode表单使用encodeURIComponent

For the output you want, use JSON.stringify . 对于所需的输出,请使用JSON.stringify It will give you a valid JSON string (including delimiters and escaped delimiters), with all special characters encoded by an escape sequence. 它将为您提供有效的JSON字符串(包括定界符和转义的定界符),所有特殊字符均由转义序列编码。

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

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