AJAX响应问题

AJAX response problem

本文关键字:问题 响应 AJAX      更新时间:2023-09-26

我有这个函数:

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();

请求发送正常,但是无法提取响应。

  • 显示xmlhttp对象中不存在readyState

当我运行你的代码时,我得到"this。XMLHTTP未定义"

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

您已经在xmlhttp对象内部了。这一行应该是:

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

根据这次讨论,这可能是Firebug中的一个bug。

你用的是什么版本的firebug ?

  • 更新Firebug
  • 尝试与其他浏览器相同?现在几乎所有的浏览器都有一个开发者控制台。