gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 03:01:52 24 4
gpt4 key购买 nike

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

最佳答案

使用 context option ,像这样:

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

context 选项决定了调用回调的上下文......因此它决定了 this 在该函数中指的是什么。

关于javascript - 如何在jquery ajax成功回调函数中传递上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3863536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com