【问题标题】:.ajaxStart and .ajaxStop not firing for some odd reason.ajaxStart 和 .ajaxStop 由于某些奇怪的原因没有触发
【发布时间】:2013-03-03 19:50:16
【问题描述】:

当发出 ajax 请求时,我将“加载”类附加到正文中,这样我就可以显示进度动画... 由于某种原因,函数 ae 从未调用过,我也看不到任何警报...

function setLoadingPanel()
{
    var timer;
    var body = $("body");

    alert("Set AJAX HOOKS...");
    $("body").on({
        ajaxStart: function ()
        {
            alert("AJAX START");
            //timer = setTimeout(function () { body.addClass("loading"); }, 50)
        },
        ajaxStop: function ()
        {
            alert("AJAX STOP!!!");
            //$(this).removeClass("loading");
            //clearTimeout(timer);
        }
    });
}

我已设置警报以尝试查看是否调用了挂钩,但由于某种原因没有弹出警报。

这是我使用简单的 AJAX.ActionLink 助手的 ajax 请求:

@{
    var ajaxDialogoptions = new AjaxOptions()
    {
        HttpMethod = "GET",
        InsertionMode = InsertionMode.Replace,
        UpdateTargetId = "DialogContainer",
        OnComplete = "OpenDialog('DialogContainer');"
    };
}
@Ajax.ActionLink(Model.AddNewItemButtonTitle, Model.AddActionName, Model.AddActionController, Model.AddActionRoutValues, ajaxDialogoptions, new { Class = "btn btn-primary anti-align-rtl" })

这是我的脚本包:

            Bundle bundle = new Bundle("~/Scripts/jsRTL");

            bundle.AddFile("~/Scripts/Common/jquery-1.9.1.min.js");
            bundle.AddFile("~/Scripts/Common/jquery-ui-1.10.1.custom.min.js");
            bundle.AddFile("~/Scripts/Common/jquery.unobtrusive-ajax.min.js");
            bundle.AddFile("~/Scripts/Validator/jquery.validate.min.js");
            bundle.AddFile("~/Scripts/Validator/jquery.validate.unobtrusive.min.js");
            bundle.AddFile("~/Scripts/Globalize/globalize.js");
            bundle.AddFile("~/Scripts/Globalize/globalize.culture.en-US.js");
            bundle.AddFile("~/Scripts/Globalize/globalize.culture.he.js");
            bundle.AddFile("~/Scripts/Globalize/globalize.culture.he-IL.js");
            bundle.AddFile("~/Scripts/Bootstrap/bootstrap-rtl.js");
            bundle.AddFile("~/Scripts/Common/Common.js");

【问题讨论】:

    标签: jquery ajax asp.net-mvc model-view-controller


    【解决方案1】:

    尝试将 ajaxStart- 和 ajaxStop-Handler 添加到文档中,如下所示:

    $(document).ajaxStart(function () {
        alert("AJAX START");
        //timer = setTimeout(function () { body.addClass("loading"); }, 50)
    });
    $(document).ajaxStop(function () {
        alert("AJAX STOP!!!");
        //$(this).removeClass("loading");
        //clearTimeout(timer);
    });
    

    here:

    然而,从 jQuery 1.8 开始,.ajaxStart() 方法只能附加到文档。

    【讨论】:

      【解决方案2】:

      我注意到您使用的是 jQuery 1.9。确保您已升级您的 jquery.unobtrusive-ajax.min.js 以便与它兼容,因为 ASP.NET MVC 4 附带的初始版本不像它依赖于已不存在的 .live() 那样。

      【讨论】:

      • 其实你也是正确的 :).. 它已经过时了,@Philipp M 关于 $(document) vs. $("body") 是正确的
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多