从 MVC Action 方法调用 jquery 函数

Call jquery function from MVC Action method

本文关键字:jquery 函数 调用 方法 MVC Action      更新时间:2023-09-26
function AddScheduledCandidate() {
if (ValidateScheduledCandidate()) {
    InsertGoogleEvent($('#comment').val(), $('#SchDateTime').val() );
    $.post(WebsiteURL + 'VideoRepository/AddScheduledCandidate', { id: $('#hdnId').val() }, "json")
                            .done(function (response) {
                                    window.location.href = response.Url;
                            })
    }
}

上面代码中的问题是我的页面在插入谷歌日历信息之前被重定向。这是因为我知道jquery代码不会逐行执行,并且在完成谷歌日历事件之前,会触发重定向代码并且不会插入谷歌日历记录。任何帮助将不胜感激。

谢谢

似乎插入谷歌事件是异步服务器调用,因此请尝试以下操作来同步调用。

function AddScheduledCandidate() {
if (ValidateScheduledCandidate()) {

$.post(WebsiteURL + 'VideoRepository/AddScheduledCandidate', { id: $('#hdnId').val() }, "json")
                        .done(function (response) {
 InsertGoogleEvent($('#comment').val(), $('#SchDateTime').val() );
                           window.location.href = response.Url;
                        })
}
}