gpt4 book ai didi

javascript - Jquery Ajax 回调不起作用调用成功和失败

转载 作者:行者123 更新时间:2023-12-03 10:05:08 24 4
gpt4 key购买 nike

我正在尝试将按钮文本设置为成功时“电子邮件已发送”或失败时“电子邮件发送失败”。我正在使用ajax调用MVC中的方法。

对 MVC 的调用工作正常,但我的代码甚至在 json 运行之前就调用了 setButtonSuccess 和 setButtonFailed?

这是我的代码:

$('input[type=button]').click(function () {
bookingID = $(this).closest('tr').attr('id');

sendEmail(bookingID, this);
});

function sendEmail(id, thisContext) {
var data = JSON.stringify({ 'id': id });

/*******
This calls setButtonSuccess AND setButtonFailed which is wrong
I want to execute only setButtonSuccess OR setButtonFailed depending on whether successful or not
*******/
jsonPOST("~Booking/ResendEmail", data, setButtonSuccess(thisContext, "Email Sent"), setButtonFailed(thisContext, "Email Failed"),false);
};

function setButtonSuccess(thisContext, buttonValue) {
$(thisContext).val(buttonValue);
$(thisContext).addClass("btn btn-success");
};

function setButtonFailed(thisContext, buttonValue) {
$(thisContext).val(buttonValue);
$(thisContext).addClass("btn btn-warning");
};

function jsonPOST (pWebServiceFunction, pData, pOnCallbackSuccess, pOnCallbackFailed, async) {
$.ajax({
type: "POST",
url: url + pWebServiceFunction,
data: pData,
contentType: "application/raw; charset=utf-8", dataType: "json",
async:async,
processdata: true,
success: function (msg) {
if (msg.success === true) {
pOnCallbackSuccess(msg);
}
else {
pOnCallbackFailed(url + pWebServiceFunction);
}
},
error: function (xhr, ajaxOptions, thrownError) //When Service call fails
{
pOnCallbackFailed(url + pWebServiceFunction, pData, xhr.status, xhr.statusText, xhr.responseText);
}
});
};

谢谢

最佳答案

您将立即调用这些函数,而不是传递稍后调用它们的函数。应该是:

jsonPOST("~Booking/ResendEmail", data, function() {
setButtonSuccess(thisContext, "Email Sent");
}, function() {
setButtonFailed(thisContext, "Email Failed");
}, false);

关于javascript - Jquery Ajax 回调不起作用调用成功和失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30392856/

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