xmlHttpRequest显示eurosign作为问号 [英] xmlHttpRequest displays eurosign as questionmark

查看:181
本文介绍了xmlHttpRequest显示eurosign作为问号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个脚本(从dynamicdrive)动态地填充div:

  var bustcachevar = 1 //突破潜力初次请求后缓存外部页面? (1 =是,0 =否)
var loadedobjects =
var rootdomain =http://+ window.location.hostname
var bustcacheparameter =

函数ajaxpage(url,containerid){
var page_request = false
if(window.XMLHttpRequest)//如果Mozilla,Safari等
page_request = new XMLHttpRequest();
else if(window.ActiveXObject){//如果IE
尝试{
page_request = new ActiveXObject(Msxml2.XMLHTTP);
}
catch(e){
try {
page_request = new ActiveXObject(Microsoft.XMLHTTP);
}
catch(e){}
}
}
else
return false
page_request.onreadystatechange = function(){
loadpage(page_request,containerid)
}

if(bustcachevar)//如果外部页面的bust缓存
bustcacheparameter =(url.indexOf(?)!= -1)? &+ new Date()。getTime():?+ new Date()。getTime()
page_request.open('GET',url + bustcacheparameter,true)
page_request。 send(null)
}

函数loadpage(page_request,containerid){
if(page_request.readyState == 4&(page_request.status == 200 || window.location.href.indexOf(http)== - 1))
document.getElementById(containerid).innerHTML = page_request.responseText
}

一切正常,直到我加载一个例如欧元的页面。
代码页在页面上设置正确,但显示问号。
我不知道足够的JavaScript来解决这个问题。



提前感谢任何建议!



注意:感谢朋友我现在知道,使用UTF-8中的此脚本保存要加载的文件可以解决问题。但是我无法确定我加载的每个页面都是UTF-8编码的,所以我的问题是:



脚本是否设置了正确的字符集?有没有办法让脚本适应要加载的文件的代码页?

解决方案

这是我的假设我认为这是您的更新确认):




  • 当您编写正在加载的远程文档时,您只需打开编辑器,点击键盘上的€符号并保存。由于您从未选择任何编码,因此您的编辑器使用ANSI代码页。这里的问题是:ANSI代码页基本上取决于你居住的地方。在西欧, Win-1252 是受欢迎的选择,并将欧元符号编码为 0x80


  • 当您将目标HTML文档写入要插入的位置时,您将完全一样并获得Win-1252文档。但是,网络服务器不知道编码是什么。很多时候,它会默认为 ISO-8859-1 ,发生这种情况ISO-8859-1甚至没有欧元符号!


  • JavaScript读取 0x80 并写入 0x80


  • 浏览器发现 0x80 据称是ISO-8859-1的HTML文档。在这样的编码中, 0x80 实际上是空白的。




所以你不需要修复你的JavaScript代码(没有什么可修复的,主要是因为没有错误)。您需要了解您的网站的编码是什么,并生成实际使用这种编码的文件(高级编辑将让您选择)。


I use this script (from dynamicdrive) to dynamically fill div with id:

    var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
    var loadedobjects=""
    var rootdomain="http://"+window.location.hostname
    var bustcacheparameter=""

    function ajaxpage(url, containerid){
        var page_request = false
        if (window.XMLHttpRequest) // if Mozilla, Safari etc
        page_request = new XMLHttpRequest();
        else if (window.ActiveXObject){ // if IE
            try {
                page_request = new ActiveXObject("Msxml2.XMLHTTP");
                }
            catch (e){
                try{
                    page_request = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                catch (e){}
            }
        }
        else
        return false
        page_request.onreadystatechange=function(){
            loadpage(page_request, containerid)
        }

        if (bustcachevar) //if bust caching of external page
        bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
        page_request.open('GET', url+bustcacheparameter, true)
        page_request.send(null)
    }

    function loadpage(page_request, containerid){
        if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
        document.getElementById(containerid).innerHTML=page_request.responseText
    }

Everything works fine until I load a page with for example a euro-sign in it. Codepage's are set correctly on the page but it displays a questionmark. I don't know enough javascript to fix this problem.

Thanks in advance for any advice!

NOTE: Thanks to friend I now know that saving the file you want to load using this script in UTF-8 fixes the problem. But I can't be sure that every page I load is UTF-8 encoded so my question is:

is there a way for the script to set the right charset? Is there a way to let the script adapt to the codepage of the file you want to load?

解决方案

This is my hypothesis (and I think it's been confirmed by your updates):

  • When you write the remote document you are loading, you just open your editor, hit the € symbol in your keyboard and save. Since you never picked any encoding, your editor used the ANSI code page. And here's the issue: the ANSI code page basically depends on where you live. In Western Europe, Win-1252 is a popular choice and encodes the euro symbol as 0x80.

  • When you write the target HTML doc where you want to insert it, you do exactly the same and get a Win-1252 document. However, the webserver doesn't know what the encoding is. Many times, it'll default to something like ISO-8859-1 and it happens that ISO-8859-1 does not even have an euro symbol!

  • JavaScript reads 0x80 and writes 0x80.

  • The browser finds 0x80 in an HTML document that's supposedly ISO-8859-1. In such encoding, the 0x80 is actually blank.

So you don't have to fix your JavaScript code (there's nothing fixable there, mainly because there's nothing wrong there). You need to find out what your site's encoding is and generate files that actually use such encoding (advanced editors will let you choose).

这篇关于xmlHttpRequest显示eurosign作为问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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