使用HttpRequest对象使用My Own Callback

时间:2009-10-12 22:06:56

标签: javascript callback httprequest

我正在编写一个不使用库的Http请求(另一个脚本有混淆......)

但是我的对象范围有问题。下面是调用脚本,然后是Ajax_Request对象。

function loadCard(e) {
var element = e.target;
if($('overlay')) {
    return false; //something is already over the layout
}

var card    =   '/card/'+element.id;
var option  = {method:'post', parameters:'test', async:true}

loadOverlay();
var ajax = new Ajax_Request(card, option);

}

// Ajax_Request

function Ajax_Request(url, options) {

if(typeof url !== 'undefined') {
    this.url = url;
}

if(typeof options.method !== 'undefined') {
    this.method = options.method;
} else {
    this.method = 'get';
}

if(typeof options.parameters !== 'undefined') {
    this.parameters = options.parameters;
}

if(typeof options.async !== 'undefined') {
    this.async = true;
} else {
    this.async = false;
}

if(window.XMLHttpRequest) {
    this.request = new XMLHttpRequest();
} //check for MS browser

this.makeRequest = function() {
    try {
        this.request.onreadystatechange = this.checkReadyState;
        this.request.open(this.method, this.url, this.async);
        if(this.method == 'post') {
            this.request.send(this.parameters);
        } else {
            this.request.send(null);
        }
    } catch(err) {
        alert(err);
    }
}

this.setResponse = function(r) {
    alert(r)
    this.response = r;
}

this.getResponse = function() {
    return this.responseText;
}


this.checkReadyState = function(r) {
    switch(this.readyState) {
        case 4:
        //Represents a "loaded" state in which the response has been completely received.
        if(this.status == 200) {
            this.setResponse(this.responseText)
        }

        ...

    }

  }
}

我正在尝试设置对属性的响应,以便我的调用对象可以使用它。 但是当我尝试调用this.setResponse()时,我得到一个错误,它是未定义的。 如何正确地将onreadystatechange回调绑定到我的程序?

否则脚本会正确返回数据,我可以直接输出它,但我需要更多的灵活性。

由于 富

3 个答案:

答案 0 :(得分:1)

发生这种情况是因为checkReadyState函数this内部实际上代表XMLHttPRequest实例而不是Ajax_Request对象,因此this.setResponse未定义。为了引用对象的方法,你必须使用一个小技巧:var that = this

function Ajax_Request(url, options) {
    var that = this;

    ...

    this.checkReadyState = function (r) {
        switch(this.readyState) {
            case 4:
            if(this.status == 200) {
                    // "this" refers to the XMLHttpRequest, 
                    // but "that" refers your custom  Ajax object
                    that.setResponse(this.responseText)
            }

        ...
        }
    }
}

答案 1 :(得分:0)

我不确定这是否是问题,但你不应该在构造函数中引用Ajax_Request。请改用this。 (this引用实际的对象实例 - Ajax_Request引用对象构造函数。)

this.makeRequest = function() {
        try {
                this.request.onreadystatechange = this.checkReadyState;
                this.request.open(this.method, this.url, this.async);
                if(this.method == 'post') {
                        this.request.send(this.parameters);
                } else {
                        this.request.send(null);
                }
        } catch(err) {
                alert(err);
        }
};

答案 2 :(得分:0)

在this.checkReadyState中,尝试将this.setResponse(this.responseText)更改为this.setResponse(this.request.responseText);