我对ajax不熟,因为要维护一个网站,不想花太多时间去看细节,请各位帮忙看一下。
我简单描述一下:
点击一个超链接(链接到一个html文件),就会出来一个效果很炫的框显示这个html文件的内容。html文件存储用gb2312,其中也用meta标签指定了content-type是gb2312,但是显示在框中的内容是乱码。我google发现ajax默认都用utf8,我这种情况可以在服务端用php的header()指定content-type,我不懂的是,ajax调用的是一个html而不是php,那我应该在什么地方指定content-type?
下面是一些辅助信息:
我用httpfox查看,发现点击后发生了一次ajax调用:(Request-Line) POST /lab/article/2010/11/1290394275.html HTTP/1.1Host localhostUser-Agent Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language en-us,en;q=0.5Accept-Encoding gzip,deflateAccept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7Keep-Alive 300Connection keep-aliveX-Requested-With XMLHttpRequestX-Prototype-Version 1.4.0Content-Type application/x-www-form-urlencoded; charset=UTF-8Referer http://localhost/lab/index.phpContent-Length 0Cookie PHPSESSID=cjb5uaf341hcq7adm38je460n6Pragma no-cacheCache-Control no-cache在ajax response中,Content-Type中的charset是utf8的。
我看了下代码,感觉有用的是这几行:// Onload, make all links that need to trigger a lightbox active
function initialize(){
    addLightboxMarkup();
    lbox = document.getElementsByClassName('lbOn');
    for(i = 0; i < lbox.length; i++) {
        valid = new lightbox(lbox[i]);
    }
}这里把所有class为lnOn的超链接做了处理。ajax可能是:    // Begin Ajax request based off of the href of the clicked linked
    loadInfo: function() {
        var myAjax = new Ajax.Request(
        this.content,
        {method: 'post', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
        );    },    // Display Ajax response
    processInfo: function(response){
        info = "<div id='lbContent'>" + response.responseText + "</div>";
        new Insertion.Before($('lbLoadMessage'), info)
        $('lightbox').className = "done";
        this.actions();
    },    // Search through new links within the lightbox, and attach click event
    actions: function(){
        lbActions = document.getElementsByClassName('lbAction');        for(i = 0; i < lbActions.length; i++) {
            Event.observe(lbActions[i], 'click', this[lbActions[i].rel].bindAsEventListener(this), false);
            lbActions[i].onclick = function(){return false;};
        }    },

解决方案 »

  1.   

    1. 服务器支持 POST 数据到html后缀的文件吗?用GET 看一下 method: 'get'
    2. 所请求的html文件本身用utf8编码存储(记事本打开,另存为,编码选utf-8), 并且html文件中<meta>中设置charset为utf-8
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
      

  2.   

    嗯,第二种方法我正在使用,只是临时的解决方法。这个html文件内容是新闻,管理员发布新闻,后台php所新闻内容存储成一个html文件并更新新闻列表,用户点击新闻列表,就是我原帖的那个情况。
      

  3.   

    另外请问一楼,为什么建议换成get?因为我把网站源代码下载到本地测试,改着代码突然问题就没了,怎么也不能重现,后来偶然发现换成get就没乱码了,甚至新闻内容不管是utf8还是gbk都能正常显示,换成post又变成乱码了,请问这中间区别在哪里?另外,我现在问题已经解决了,我自己写了个php文件,把原来ajax请求的html文件作为参数传递给php,在php中先header设置一下response header中的编码,然后再把html文件的内容输出出来。