我对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;};
} },
我简单描述一下:
点击一个超链接(链接到一个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;};
} },
解决方案 »
- 请教:缺少对象的问题
- again Javascript中利用WScript.Shell的变量中含有空格问题?
- insertRow()插入的动态表格激发的onmouseover 能alert出来东西却不能改变动态表格行的颜色。是怎么回事情
- 请问这个可以用javascript实现吗?
- 大家帮我分析下,下面的代码的意思吧
- 怎样在Javascript中读取一个iframe的内容
- javascript怎么接收地址后面的参数值
- 怎样实现点击连接后出现“目标另存为...”的对话框?
- 关于动态添加表格和引用css的问题。
- 如何实现如图所示效果
- onMouseover和onMouseout的问题
- javascript怎么新建一个DIV呢?
2. 所请求的html文件本身用utf8编码存储(记事本打开,另存为,编码选utf-8), 并且html文件中<meta>中设置charset为utf-8
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />