Javascript 如何在jquery ajax成功回调函数中传递上下文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3863536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass context in jquery ajax success callback function
提问by rajakvk
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 方法中获取 Box 对象。如何在 exeSuccess 方法中传递 Box 对象?
回答by Nick Craver
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.
上下文选项决定了调用回调的上下文......因此它决定了this
该函数内部引用的内容。