我可以在 Javascript 中使用什么函数将一种字符编码转换为另一种字符编码?
我可以在 Javascript 或 jQuery 中使用哪些内置或用户定义函数将一种字符编码转换为另一种字符编码?
例如, 从“utf-8”到“windows-1256” 或者 从“windows-1256”到“utf-8”
实际用途是,如果您有一个具有特定字符编码(如“windows-1256”)的 php 页面,您无法根据业务需求和何时更改它您使用ajax使用json从数据库发送块数据,该数据仅使用“utf-8”编码,因此您需要将json的输出转换为这种编码,以便字符和字符串能够很好地显示
提前致谢 .....
what the built-in or user-defined function that I can use in Javascript or jQuery to convert from one character encoding to another?
For Example,
FROM "utf-8" TO "windows-1256"
OR
FROM "windows-1256" TO "utf-8"
A practical use of that is if you have a php page with specific character encoding like "windows-1256" that you could not change it according to the business needs and when you use ajax to send a block data from database using json which uses "utf-8" encoding only so you need to convert the ouput of json to this encoding so that the characters and the strings will be displayed well
Thanks in advance .....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 JavaScript 运行时环境的角度来看,实际上不存在字符编码这样的东西——混乱的编码已经从你身边抽象出来了。根据 spec,所有 JS 源文本都会被解释作为 Unicode 字符,并且所有
String
都是 Unicode。因此,JavaScript 中没有办法以 Unicode 以外的任何方式表示字符。查看
String
实例上可用的方法< /a> – 你会看到没有任何与字符编码相关的内容。由于 JavaScript 以 Unicode 运行,并且所有 JavaScript 字符串都以 Unicode 存储,因此所有 AJAX 调用都将以 Unicode 形式通过线路传输。来自 jQuery AJAX 文档:
您的 PHP 脚本必须处理来自 AJAX 调用的 Unicode 输入。
From the standpoint of a JavaScript runtime environment, there's really no such thing as character encodings – the messiness of encodings is abstracted away from you. By spec, all JS source text is interpreted as Unicode characters, and all
String
s are Unicode.As such, there's no way in JavaScript to represent characters in anything other than Unicode. Look at the methods available on a
String
instance – you'll see there's nothing related to character encoding.Because JavaScript runs in Unicode, and all JavaScript strings are stored in Unicode, all AJAX calls will be transmitted over the wire in Unicode. From the jQuery AJAX docs:
Your PHP script is going to have to cope with Unicode input from AJAX calls.