我有以下 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
之前的空格。
屏幕截图
关于jquery - Ajax 调用永远不会返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30119271/