如何在jquery ajax成功回调函数中传递上下文

时间:2022-10-23 20:39:17
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);
    }
}

I'm not getting Box object inside exeSuccess method. How to pass Box object inside exeSuccess method?

我没有收到框对象exeSuccess方法内。如何通过框对象exeSuccess方法里面?

1 个解决方案

#1


77  

Use the context option, like this:

使用上下文选项,如下所示:

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

The context option determines what context the callback is called with...so it determines what this refers to inside that function.

上下文选项确定何种情况下回调调用...所以它决定了这指的是函数内部。

#1


77  

Use the context option, like this:

使用上下文选项,如下所示:

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

The context option determines what context the callback is called with...so it determines what this refers to inside that function.

上下文选项确定何种情况下回调调用...所以它决定了这指的是函数内部。