gpt4 book ai didi

javascript - 如何将对象的副本传递给 $.ajax 的 .done() 中的函数?

转载 作者:搜寻专家 更新时间:2023-10-30 21:55:03 25 4
gpt4 key购买 nike

我有以下代码:

function doDialogAjax(link: Link. modal: Modal) {
$.ajax( link.Url,
{
cache: false,
dataType: 'html'
})
.done(onDialogDone)
.fail(onDialogFail);
}

function onDialogDone(data: any, textStatus: string, jqXHR: JQueryXHR) {
var x = data;
// I need to access link.abc and modal.def properties here
}

如何将链接对象发送到我的 onDialogDone() 函数?我似乎记得有一些方法可以发送指定的上下文信息对象,但我找不到任何这样的例子。

最佳答案

您可以使用上下文键来更改回调中 this 的值:

function doDialogAjax(link: Link. modal: Modal) {
$.ajax( link.Url,
{
cache: false,
dataType: 'html',
context: {
link: link,
modal: modal
}
})
.done(onDialogDone)
.fail(onDialogFail);
}

function onDialogDone(data: any, textStatus: string, jqXHR: JQueryXHR) {
var x = data;
// this refers to the context-object, with keys [link, modal]
console.log(this.link);
console.log(this.modal)
}

关于javascript - 如何将对象的副本传递给 $.ajax 的 .done() 中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13343557/

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