1

I am trying to create my own version of the ajax method in Jquery to see how it works:

function ajax(url, method) {
    var self = this;
    this.xhr = new XMLHttpRequest();
    this.xhr.onreadystate = function() {
        self.xhrHandler();
    }
    this.xhr.open(method, url, true);
    this.xhr.send();
}

ajax.prototype.xhrHandler = function() {
    if (this.xhr.readyState == 4) {
        console.log(this.xhr.responseText);
    }
    console.log("test");
}

It never goes into the xhrHandler function, though, since it never prints out "test". What is going on?

Edit: Here is a usage example: var ex = new ajax("www.fake.com/api/item/1/", "GET");

1
  • 1
    Can you give a usage example? Commented May 31, 2012 at 17:06

1 Answer 1

3

The handler is called onreadystatechange, not just onreadystate.

And (that's a detail), you should also test the status.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.