简体   繁体   English

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

[英]How to pass context in jquery ajax success callback function

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.我没有在 exeSuccess 方法中获取 Box 对象。 How to pass Box object inside exeSuccess method?如何在 exeSuccess 方法中传递 Box 对象?

Use the context option , like this:使用context选项,如下所示:

    $.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. context 选项决定了回调被调用的上下文......因此它决定了this在该函数内引用的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM