我有ajax oop脚本,它工作得很好,但不会回调onreadystatechange函数。下面是代码=>
function AjaxConstruct(method,file,params){
this.method = method;
this.file = file;
this.params = params;
this.http = false;
}
AjaxConstruct.prototype.ajax = function(){
if (window.XMLHttpRequest){
this.http = new XMLHttpRequest();
} else {
this.http = new ActiveXObject("Microsoft.XMLHTTP");
}
if (this.http){
this.http.open(this.method,this.file,true);
if (this.method==="POST"){
this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}
this.http.send(this.params);
this.http.onreadystatechange = function(){
if (this.http.readyState==4 && this.http.status==200){
alert("yeah");
}
};
}
};没有回调到onreadystatechange匿名函数,怎么解决?谢谢:)
调用像这样的=>方法
var ajax = new AjaxConstruct("POST","filename","params");
ajax.ajax();但是onreadystatechange没有调用:(
发布于 2012-05-30 18:05:18
我认为回调是被调用的,但是访问this.http.readyState会抛出一个错误,因为在回调中,this不会引用您的实例。
查看控制台中的错误。
只需使用this.readyState或将XMLHTTPRequest对象赋给一个局部变量,并使用该变量而不是this.http。
了解有关this的更多信息。
https://stackoverflow.com/questions/10813718
复制相似问题