1

我正在编写一个不使用库的 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 回调绑定到我的程序?

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

谢谢丰富

4

3 回答 3

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

        ...
        }
    }
}
于 2009-10-12T23:40:40.403 回答
0

我不确定这是否是问题,但你不应该Ajax_Request在构造函数中引用。改为使用this。(this指的是实际的对象实例——<code>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);
        }
};
于 2009-10-12T22:20:58.927 回答
0

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

于 2009-10-12T22:46:38.700 回答