Jquery:当我一步到位时工作,当我让它运行时出错

时间:2016-01-28 03:43:42

标签: javascript jquery asp.net-mvc-4

我正在为我的mvc应用程序创建角色。当我逐步完成代码时,一切正常。当我不运行调试工具时,我会收到警报(如果我试图询问xHP.responseMessage,则不确定)。有什么想法吗?

这个答案看起来可能适用,但成功是一个回调,所以我不确定它会如何应用

javascripts works when I step through doesnt if I dont

@model Microsoft.AspNet.Identity.EntityFramework.IdentityRole

<div class="row">
    <form id="frmRoleEditor" class="form-inline">

        <div class="form-group">
            @Html.LabelFor(m => m.Name, new { @class = "col-sm-3" })
            <div class="col-sm-9">
                @Html.TextBoxFor(m => m.Name, new { @class = "form-control", required = "true", minlength = "2", maxlength = "256" })
            </div>
        </div>
        <div class="form-group">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>&nbsp;
            <input type="submit" class="btn btn-success" value="Update" />
        </div>
    </form>
</div>

<script type="text/javascript">

    //$(function () {

    //    $("#frmRoleEditor").validate();

    //});

</script>

        [HttpPost]
        public JsonResult RoleEditor(IdentityRole model)
        {

            try
            {
                using (ApplicationDbContext context = new ApplicationDbContext())
                {
                    var exists = context.Roles.SingleOrDefault(r => r.Name == model.Name);

                    if (exists == null)
                    {
                        context.Roles.Add(model);
                        context.SaveChanges();
                    }

                }

                var redirectUrl = new UrlHelper(Request.RequestContext).Action("Roles", "Admin");
                return Json(new { success = true, Url = redirectUrl });

            }
            catch (Exception ex)
            {

                throw;
            }




        }

$(function () {

    $("#btnNewRole").on("click", function () {

        $("#modalRole").modal("show");

    });

    $("body").on("submit", "#frmRoleEditor", function () {

        var name = $("#Name").val();
        var model = {
            Name: name
        };

        $.ajax({
            url: "/Admin/RoleEditor",
            type: "POST",
            data: model,
            success: function (data) {
                if (data.success) {
                    window.location.href = data.Url;
                }
            },
            error: function (xHP, status, error) {
                alert('wtf');
            }
        });

    });

});

更新

  1. 它发生在Chrome或Firefox中,Edge没问题
  2. 当我查看网络标签时,我看到状态为(已取消)
  3. 如果我在控制器中设置了一个断点,我在客户端上通过服务器上的代码指示上面的链接是相关的之前得到客户端的错误代码,但我仍然不知道如何

0 个答案:

没有答案