【问题标题】:How to pass context in jquery ajax success callback function如何在jquery ajax成功回调函数中传递上下文
【发布时间】:2011-04-21 06:47:21
【问题描述】:
var Box = function(){
    this.parm = {name:"rajakvk",year:2010};
    Box.prototype.jspCall = function() {
        $.ajax({
            type: "post",
            url: "some url",
            success: this.exeSuccess,
            error: this.exeError,
            complete: this.exeComplete
        });
    }
    this.exeSuccess = function(){
        alert(this.parm.name);
    }
}

我没有在 exeSuccess 方法中获取 Box 对象。如何在 exeSuccess 方法中传递 Box 对象?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    使用context option,像这样:

        $.ajax({
            context: this,
            type: "post",
            url: "some url",
            success: this.exeSuccess,
            error: this.exeError,
            complete: this.exeComplete
        });
    

    上下文选项决定了调用回调的上下文......因此它决定了this在该函数中引用的内容。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多