因为网页发评论时程序使用XMLHttpRequest,在IE9下中文就显示乱码,据说IE8下是好的。
我查找了些资料,大致是说,XMLHttpRequest是默认网页为utf-8格式,如果网页不是utf-8中文就会发生乱码。我后来修改了一个地方就把问题解决了,因为另一个地方程序里已经有了。这里把两个地方都显示一下:
1)这里编码是gb2312,通过url发送信息,先对url使用encodeURI:
url = encodeURI(url);
if(window.XMLHttpRequest) { //Mozilla 浏览器
AJAX.http_request = new XMLHttpRequest();
if (AJAX.http_request.overrideMimeType) {//设置MiME类别
// AJAX.http_request.overrideMimeType('text/xml;charset=gb2312'); // changed by Mia on Oct. 17
}
}else if (window.ActiveXObject) { // IE浏览器
try {
AJAX.http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
AJAX.http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!AJAX.http_request) {
window.alert("不能创建XMLHttpRequest对象实例.");
return false;
}
AJAX.http_request.onreadystatechange = AJAX.processRequest;
AJAX.http_request.open("GET", url+"&"+Math.random(), true);
AJAX.http_request.send(null);
2) 从上面得到数据到$content后,将数据用iconv由utf-8转换为gb2312后再写入数据库。
$content = iconv("utf-8","gb2312", $content);
另外,网页定义编码时php用这个header('Content-Type: text/html; charset='gb2312');