在utf-8文件(javascript XMLHttpRequest)中读取不良的欧洲字符 [英] reading in utf-8 file (javascript XMLHttpRequest) gives bad european characters

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

问题描述

任何人都可以帮助?我使用XMLHttpRequest使用JavaScript读取UTF-8文件的小程序。这个文件有诸如miércolessábado等之类的欧洲字符。注意口音..



但是当被读入...字符都被弄乱了..我检查了文件,它是完美的..它必须是阅读的程序..



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



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



但是当返回并通过下面的过程读取它就像这样(注意sabado和miercoles中的有趣的人物)



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



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

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

var request = new XMLHttpRequest();
request.open(GET,path,false);
request.setRequestHeader -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;

//这里有欧洲字符,读取

print(request.responseText);


return request.responseText;


解决方案

p>我想你必须使用不同的方式打印字符,例如,请参阅这个讨论

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


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

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");

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");

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;

解决方案

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天全站免登陆