nanobash
nanobash

Reputation: 5500

ajax onreadystatechange function

I've ajax oop script which is working perfectly but doesn't callback onreadystatechange function. here is code =>

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");
    }
};
}
};

it doesn't callback onreadystatechange anonymous function , how can be it solved ? thanks :)

invoking method like this =>

var ajax = new AjaxConstruct("POST","filename","params");
ajax.ajax();

but onreadystatechange doesn't called :(

Upvotes: 0

Views: 3797

Answers (1)

Felix Kling
Felix Kling

Reputation: 816334

I think the callback is called, but accessing this.http.readyState will throw an error, since inside the callback, this does not refer to your instance.

Have a look at the console for errors.

Just use this.readyState or assign the XMLHTTPRequest object to a local variable and use that instead of this.http.

Learn more about this.

Upvotes: 1

Related Questions