gpt4 book ai didi

javascript - 有没有办法将 OnSuccess 脚本添加到我的 Ajax 调用中

转载 作者:行者123 更新时间:2023-11-30 17:28:07 25 4
gpt4 key购买 nike

我有以下脚本:

$.ajax({
url: '/Switch/showOptions',
data: {
switchid: "331",

},
type: 'get',
success: function (html) {
$('#showoptions').html(html);
$("#showoptions").dialog("show"); //This could also be dialog("open") depending on the version of jquery ui.
OnSuccess('createsuccess')
},
});

我想做的是在显示对话框后触发 OnSuccess 脚本,目前我收到一个异常,说 OnSuccess 没有定义?那么任何人都可以建议我如何为我的 Ajax 调用触发 OnSuccess 脚本吗?

最佳答案

尝试使用 jQuery.when()如所述in this answer here :

Provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events.

考虑到这一点,这里是你的代码修改后使用 jQuery.when():

var ajax_action = $.ajax({
url: '/Switch/showOptions',
data: {
switchid: "331",

},
type: 'get',
success: function (html) {
$('#showoptions').html(html);
$("#showoptions").dialog("show"); //This could also be dialog("open") depending on the version of jquery ui.
},
});

$.when(ajax_action).then(function(data, textStatus, jqXHR) { OnSuccess('createsuccess'); });

或者,如果您只是想测试这个概念,请将 jQuery.when() 更改为:

$.when(ajax_action).then(function(data, textStatus, jqXHR) { alert('I am a success!'); });

编辑:最后一次编辑以尝试解决原始发帖人的要求。他们想要触发一个名为 createsuccess 的脚本,所以只需这样做:

$.when(ajax_action).then(function(data, textStatus, jqXHR) { createsuccess(); });

关于javascript - 有没有办法将 OnSuccess 脚本添加到我的 Ajax 调用中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23818879/

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