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

问题描述:

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对象?

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

使用 context 选项,如下所示:

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.