将我自己的回调与 HttpRequest 对象结合使用

发布于 2024-08-07 07:17:44 字数 1941 浏览 3 评论 0原文

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

否则,脚本会正确返回数据,我可以简单地在那里输出它,但我需要更多的灵活性。

谢谢 富有的

I'm writing an Http Request without the use of a library (another script was having conflits...)

But Im having trouble with the scope of my object. Below is the calling script, then the Ajax_Request object follows.

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

        ...

    }

  }
}

I'm trying to set the response to a property so my calling object can work with it.
But when I try to call this.setResponse(), I get an error that it's undefined.
How can I tie the onreadystatechange callback to my program properly?

The script otherwise returns the data properly, and I could simply output it right there, but I need a bit more flexibility.

Thanks
Rich

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尤怨 2024-08-14 07:17:44

这种情况发生在您身上,是因为在 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)
            }

        ...
        }
    }
}

This is happening to you because inside the checkReadyState function this actually represents the XMLHttPRequest instance not you Ajax_Request object, thus this.setResponse is undefined. In order to reference your object´s method you have to use a little trick: 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)
            }

        ...
        }
    }
}
风渺 2024-08-14 07:17:44

我不确定这是否是问题所在,但您不应该在构造函数中引用 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);
        }
};

I'm not sure whether this is the problem, but you shouldn't be referring to Ajax_Request within the constructor. Use this instead. (this refers to the actual object instance—Ajax_Request refers to the object constructor.)

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);
        }
};
时光倒影 2024-08-14 07:17:44

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

In this.checkReadyState, try changing this.setResponse(this.responseText) to this.setResponse(this.request.responseText);.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文