+ -
当前位置:首页 → 问答吧 → lightbox的ajax请求的是一个html文件,乱码问题(php+prototypejs)

lightbox的ajax请求的是一个html文件,乱码问题(php+prototypejs)

时间:2010-11-30

来源:互联网

我对ajax不熟,因为要维护一个网站,不想花太多时间去看细节,请各位帮忙看一下。
我简单描述一下:
点击一个超链接(链接到一个html文件),就会出来一个效果很炫的框显示这个html文件的内容。html文件存储用gb2312,其中也用meta标签指定了content-type是gb2312,但是显示在框中的内容是乱码。我google发现ajax默认都用utf8,我这种情况可以在服务端用php的header()指定content-type,我不懂的是,ajax调用的是一个html而不是php,那我应该在什么地方指定content-type?
下面是一些辅助信息:
我用httpfox查看,发现点击后发生了一次ajax调用:
HTML code

(Request-Line)    POST /lab/article/2010/11/1290394275.html HTTP/1.1

Host    localhost

User-Agent    Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3

Accept    text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language    en-us,en;q=0.5

Accept-Encoding    gzip,deflate

Accept-Charset    ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive    300

Connection    keep-alive

X-Requested-With    XMLHttpRequest

X-Prototype-Version    1.4.0

Content-Type    application/x-www-form-urlencoded; charset=UTF-8

Referer    http://localhost/lab/index.php

Content-Length    0

Cookie    PHPSESSID=cjb5uaf341hcq7adm38je460n6

Pragma    no-cache

Cache-Control    no-cache


在ajax response中,Content-Type中的charset是utf8的。
我看了下代码,感觉有用的是这几行:
JScript code

// 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可能是:
JScript code

    // 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;};
        }

    },


作者: jcwKyl   发布时间: 2010-11-30

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

作者: hookee   发布时间: 2010-11-30

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

作者: jcwKyl   发布时间: 2010-11-30

热门下载

更多