AJAX响应问题

I've got this function:

function Ajax() {
    var xmlhttp, onready, open, response;

    if(window.XMLHttpRequest) {
        this.xmlhttp = new XMLHttpRequest();
    } else {
        this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    this.onready = function(onready) {
        this.xmlhttp.onreadystatechange = function() {
            if ( this.xmlhttp.readyState == 4 && this.xmlhttp.status == 200) {
                onready.call();
            }
        }
    };

    this.open = function(_filename, _method) {
        this.xmlhttp.open(_method, _filename, true);
        this.xmlhttp.send(null);
    };

    this.response = function() {
        return this.xmlhttp.responseText;
    };
}


function rc() {
    var ajax = new Ajax();

    ajax.onready(function() {
        document.getElementById("comments").innerHTML = ajax.response();
    });

    ajax.open("ab.php","GET");
}

rc();

And the request is sent fine, but i can't extract the response.

  • It shows that readyState doesn't exist in xmlhttp object.

This could be a bug in Firebug, according to this discussion.

What version of firebug are you using?

Can you try

  • Updating Firebug
  • Trying the same with another browser? These days almost all browsers have a developer console.

When I run your code I am getting "this.xmlhttp is undefined" for the line:

if ( this.xmlhttp.readyState == 4 && this.xmlhttp.status == 200) 

You are already inside of the xmlhttp object. The line should be:

if ( this.readyState == 4 && this.status == 200) 

This should fix the error.