Javascript 将 ansi 转换为 utf8
我正在尝试使用此插件 jquery.csvToTable 将数据从 csv 显示到网页,cvs 文件具有日语文本的编码 ansi ,但网页具有编码 utf8 ,并且 js 无法使用 ansi ,如何转换或如果存在另一种方法 data $.get(csvFile, function(data) {
data to utf8 ,抱歉我的英语不好,非常感谢!
i'm trying with this plugin jquery.csvToTable to show data from csv to web page , cvs file has encoding ansi with Japanese text , but webpage has encoding utf8 , and js is not working with ansi , how is possible to convert or if exist another method data $.get(csvFile, function(data) {
data to utf8 , sorry for my bad English , thanks a lot !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您在 JavaScript 中处理字符串时,您正在处理 UTF-16。浏览器需要确保传递到 JavaScript 层的任何数据都已正确转换。
在本例中,因为您使用的是
$.get
,这意味着 ajax 层必须知道它正在处理什么。您需要确保您的服务器在包含 CSV 文件的 HTTP 响应中返回正确的字符集信息,以便浏览器知道字符数据的格式。完成此操作后,浏览器应该执行任何必要的转换从原始的 JavaScript 到 UTF-16。具体来说,如果您的 CSV 文件采用字符集 Windows-1252(有时人们称之为“ANSI”,尽管它不正确),这将是您的服务器应随文件返回的
Content-Type
标头:...但是如果您的内容是日语,我认为不会在 Windows-1252 中,是一个(非常有限的)拉丁字符集。如果您使用的是 Windows,则更有可能是 代码页 932,即:
。 ..或者如果您使用的是 *nix,也许 EUC-JP :
您可以在此 W3C 文档中了解有关字符集的更多信息。 (Joel Spolsky 的这篇文章也很有帮助。)有关 JavaScript 字符串的更多信息可以在在规范中找到(但基本上,每个“字符” JavaScript 字符串中的 UTF-16 单词,这意味着需要两个单词的字符在字符串中显示为两个“字符”——对于某些文本(尤其是东亚文本)来说并不理想,但在定义它时,RAM还是很珍贵的……)。
By the time you're dealing with a string in JavaScript, you're dealing with UTF-16. It's up to the browser to ensure that any data it passes to the JavaScript layer has been transformed correctly.
In this case, because you're using
$.get
, that means that the ajax layer has to know what it's dealing with. You'll need to ensure that your server is returning the correct charset information in the HTTP response containing the CSV file, so the browser knows what format the character data is in. Once you're doing that, the browser should do any necessary transformation from the original to UTF-16 for JavaScript.Specifically, if your CSV file is in the character set Windows-1252 (sometimes people call that "ANSI" although it's not correct), this would be the
Content-Type
header your server should return with the file:...but if your content is Japanese, I wouldn't think it would be in Windows-1252, which is a (very limited) Latin character set. If you're using Windows, it's more likely to be Code Page 932, which would be:
...or if you're using *nix, perhaps EUC-JP:
You can learn more about charsets in this W3C document. (This article by Joel Spolsky is also quite helpful.) Further information about JavaScript's strings can be found in the specification (but basically, each "character" in a JavaScript string is a UTF-16 word, which means characters requiring two words show up as two "characters" in the string — not ideal for some texts, particularly east-asian ones, but when it was being defined, RAM was still precious...).
我使用这个函数:
摘自: http:// /ecmanaut.blogspot.com.ar/2006/07/encoding-decoding-utf8-in-javascript.html
I use this functions:
Extracted from: http://ecmanaut.blogspot.com.ar/2006/07/encoding-decoding-utf8-in-javascript.html
希望这对您有用 ↓
http://jsfromhell.com/geral/utf-8
I hope this is useful to you ↓
http://jsfromhell.com/geral/utf-8