jquery - Ajax 调用永远不会返回

标签 jquery asp.net ajax asp.net-mvc asp.net-mvc-4

我有以下 ajax 调用:

$.ajax({
            type: "POST",
            url: urlToGetRules,
            data: { ruleName: ruleName},
        })
             .success(function (data) {
                 document.location.href = "/StudentRules/GetAllStudentRules?productId=test";
             })
             .fail(function (xhr) {
                 alert("Something went wrong!!!")
                 console.log(xhr.responseText);
             });

在我的 Controller 中,我正在 DocDb 中创建一个文档,如下所示:

[HttpPost]
public ActionResult UpsertDoc(string ruleName)
{
            StudentRule studentRule = new StudentRule() { Id = Guid.NewGuid().ToString(), StudentId = "test", Name = ruleName, RuleType = "Allow all updates", StartDate = DateTime.UtcNow.ToString() };
            _updateScheduleRulesManager.UpsertScheduleRule(studentRule);

            return Json(new { success = true });
}

这个想法是,一旦用户在“添加规则”页面中创建新规则,就返回到一个列出所有规则的页面。

上面的代码执行良好,我可以在 Docdb 中看到预期的文档,但开发人员工具中此调用的状态显示“待处理”! Success 中的代码永远不会执行。

有人可以指导我吗?

提前致谢。

最佳答案

没有 .success 处理程序。

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

您需要使用完成:

  $.ajax({
        type: "POST",
        url: '@Url.Action("UpsertDoc")',
        data: { ruleName: 'test'},
        }).done(function (data) {
            document.location.href = "/StudentRules/GetAllStudentRules?productId=test";            
        }).fail(function (xhr) {
        alert("Something went wrong!!!")
        console.log(xhr.responseText);
        });

同时删除 .done.fail 之前的空格。

屏幕截图

Screen shot

关于jquery - Ajax 调用永远不会返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30119271/

相关文章:

php - TABLE TD 中的可放置和可拖动

javascript - 数据列表 : detect if event input is launched by a click on item or a Key pressed

asp.net - 如何根据用户所处的角色创建具有不同显示的 View ?

ajax - 选择 "Choose File"按钮后预览 ImageField

javascript - 发送上传的文件和其他数据,两者都使用一个ajax调用

jquery - 在 Google Chrome 中使用 jquery 延迟加载时,动态生成的图像被请求两次

jquery - 在 WordPress 中运行 jquery 还需要什么?

asp.net - ASP.NET Web 服务命名空间子域重要吗?

asp.net GridView分页器消失了!

ajax - 如何在使用代理在 ext js 存储中发布请求时发送 JSON