异步的时候: JScript code
alert("get");
this.xmlReq.open(this.method, this.url,this.isAsync);
alert("open");
this.xmlReq.send(this.data);
在这里我使用了这两个alert,当alert("get");之前不会有(网页错误提示)
但是当open操作之后,即显示alert("open");之后就会有(网页错误提示) 同步的时候:
当this.xmlReq.open操作的时候this.isAsync为false即为异步的时候就不会出错后来调适了下,发现每次readystate的值改变的时候,就会出现一次网页报错,郁闷啊!
alert("get");
this.xmlReq.open(this.method, this.url,this.isAsync);
alert("open");
this.xmlReq.send(this.data);
在这里我使用了这两个alert,当alert("get");之前不会有(网页错误提示)
但是当open操作之后,即显示alert("open");之后就会有(网页错误提示) 同步的时候:
当this.xmlReq.open操作的时候this.isAsync为false即为异步的时候就不会出错后来调适了下,发现每次readystate的值改变的时候,就会出现一次网页报错,郁闷啊!
var readyState = this.xmlReq.readyState;//得到readyState的值
//异步处理
alert("在readyState == 4的外面");
if (this.xmlReq.status == 200) {
alert("在this.xmlReq.status == 200的时候");
if (readyState == 4) {
var dataS = "<font color='red'>error:The content type is "
+ this.xmlReq.getResponseHeader("content-type")
+ "</font>";//初始化数据
//对返回类型的判断处理
//为xml格式的数据
if (this.type == "xml"
|| this.xmlReq.getResponseHeader("content-type")
.indexOf("xml") >= 0) {
dataS = this.xmlReq.responseXML;//得到XML文档对象
} else
//为text格式时
if (this.type == "tex.....这是这个函数中的操作
if (this.xmlReq.status == 200) {这样写看看
还有你在readystate==4的时候里面用到的变量因为异步的话是边加载边执行后面的js的,如果你后面js在他没加载完的时候就用到了他,就会出错
readystate==4了以后就可以顺利的得到数据,不报错。
readystate从0=》4的过程中,变化一次就报错一次,错误都一样的,四次变换四次错误
如果有的话需要注意,this.stateChange定义时this是想指向你定义的Ajax类,但现在触发onreadystatechange的时候,stateChange的指针已经指向了this.xmlReq,需要做一个this指针的绑定Function.prototype.bind = function(bind)
{
var fn = this;
return function()
{
return fn.apply(bind);
}
}
this.xmlReq.onreadystatechange = this.stateChange.bind(this);
this.xmlReq.onreadystatechange = function() {
alert("onreadystatechange");
owner.stateChange.call(owner);//调用stateChange方法,参数为owner
};回调函数是这样的
所以你用异步不报错,而用同步就报错。解决办法:判断运行方式,如果是同步跳过onreadystatechange 环节。
this.xmlReq.onreadystatechange = function() {
alert("onreadystatechange");
owner.stateChange.call(owner);//调用stateChange方法,参数为owner
};
}
this.isAsync为false即为异步改为this.isAsync为false即为同步
这才是我写的代码,只是发帖的时候写错了,不好意思