ajax 乱码..快来帮帮忙啊

来源:百度知道 编辑:UC知道 时间:2024/06/22 22:33:52
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body onLoad="MyShow()">
<script type ="text/javascript" language ="javascript" >
var req; //定义变量,用来创建xmlhttprequest对象
function creatReq() {// 创建xmlhttprequest,ajax开始
var url="jss.php"; //要请求的服务端地址
if(window.XMLHttpRequest) {//非IE浏览器,用xmlhttprequest对象创建
req=new XMLHttpReque

在req.send(null);前面
加上这句!
req.setRequestHead("Content-Type","application/x-www-urlencode;charset=gb2312");

在你的请求后台文件:jss.php 在输出前加上:header('Content-Type:text/html;charset=GB2312');这句。

建议楼主用js(JQuery,mootools,protoype)框架,连ajax对象都要手写划不来。

发现好多人都在推荐框架,框架固然好用,但是用多了框架你就无法深入了解JS的精髓。
我不知道你那里出了乱码,但是需要告诉你的事使用Ajax向服务器段传递中文字符你需要encode
这是我自己的Ajax类中的中文处理代码片断,希望对你有用:
this.request.open(this.method,this.url);
this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
if(this.data){
this.data = encodeURI(this.data);
this.data = encodeURI(this.data);
this.request.send(this.data);
}else{
this.request.send();
}
鉴于你使用的是php,php我并不精通,但是肯定要在服务端进行字符编码转换。
另一个问题。其实并不难,只是你没有了解XmlHttpRequest的生命周期。readystate有5个状态,0-4,如果你需要显示正在加载之类的提示,那么你就在前4个状态中0-3,写回调方法。

这里的乱码问题需要从js中就要开始解决,
给你个列子,你看看,又需要再说
var