读入 utf-8 文件 (javascript XMLHttpRequest) 给出坏的欧洲字符 [英] reading in utf-8 file (javascript XMLHttpRequest) gives bad european characters

查看:18
本文介绍了读入 utf-8 文件 (javascript XMLHttpRequest) 给出坏的欧洲字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮忙吗?我有一个小程序可以使用 XMLHttpRequest 用 javascript 读取 UTF-8 文件..这个文件有欧洲字符,比如 miércoles sábado 等..注意口音..

can anyone help? I have small procedure to read in an UTF-8 file with javascript using XMLHttpRequest.. this file has european characters like miércoles sábado etc.. Notice the accents..

但是读入的时候..字符全乱了..我查过文件,完美..一定是读入的程序..

But when being read in .. the characters are all messed up.. I have checked the file and it is perfect.. it must be the procedure for reading in..

这里有一个例子,我有一个包含文件的例子,这个文件是完美的,它恰好是 javascript 但没关系..任何带有特殊字符的 UTF-8 编码文件都会给我同样的问题

heres an example i have file that contains, the file is perfect, it happens to be javascript but it doesn't matter.. any UTF-8 encoding file with special characters gives me the same issue

this.weekDays = new Array("Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado", "Domingo");

this.weekDays = new Array("Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado", "Domingo");

但是当返回并通过下面的程序读取时,它是这样的(注意sabado和miercoles中的有趣字符)

but when returned and read by the procedure below it is like this (notice the funny characters in sabado and miercoles)

this.weekDays = new Array("Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado", "Domingo");

this.weekDays = new Array("Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado", "Domingo");

这是我的程序 - 它非常小...

Here is my procedure - its very small...

var contentType = "application/x-www-form-urlencoded; charset=utf-8";

var request = new XMLHttpRequest(); 
request.open("GET", path, false);
request.setRequestHeader('Content-type', contentType)

if (request.overrideMimeType) request.overrideMimeType(contentType);

try { request.send(null); }
catch (e) { return null; }
if (request.status == 500 || request.status == 404 || request.status == 2 || (request.status == 0 && request.responseText == '')) return null;

//PROBLEM HERE is with european charcters that are read in

print(request.responseText);


return request.responseText;

推荐答案

似乎这个答案虽然被接受,但并不理想,所以对于遇到类似问题的任何人,请查看 里卡多的回答

Seems that this answer, although accepted, is suboptimal, so for anyone coming here with a similar problem, check out Ricardo's answer

我认为你必须使用不同的方式来打印字符,例如查看这个讨论:

I think you have to use a different way to print the characters, for example, see the code at the end of this discussion:

<script>
  function getUnicode(num) {
    num = num.toString(16);
    if (num.length < 3) {
      for ( var i = num.length; i < 4; i++) {
        num = '0' + num;
      }
    }
    return ( "&#" + num + ";" );
  }

  for ( var i = 0; i < 65355; i++) {
    document.write(getUnicode(i));
  }
</script>

这篇关于读入 utf-8 文件 (javascript XMLHttpRequest) 给出坏的欧洲字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆