【发布时间】:2020-12-12 17:12:13
【问题描述】:
我有一个 aps.net MVC5 网络应用程序,我有一个看起来像这样的控制器操作发布方法,
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Set(int id)
{
DbHelper dbHelper = new DbHelper();
dbHelper.Update<News>("update news set ispublished = 1, publishdate = @PublishDate where newsid = @NewsId", new { NewsId = id, PublishDate = DateTime.Now });
return Json(new { success = true, message = "added successfully!", redirectToUrl = Url.Action("NewsList", "Home") }, JsonRequestBehavior.AllowGet);
}
现在,当我从 View 调用此方法时,
$(document).on("click", ".set", function () {
var mId = $(this).attr("data-model-id");
$.ajax({
url: "@Url.Action("Set","Home")",
data: { "id": mId },
contentType: 'application/json',
type: 'POST',
dataType:'json',
success: function (response) {
if (response.success) {
window.location.href = response.redirectToUrl;
dt.ajax.reload();
$.notify(data.message, {
globalPosition: "top center",
className: "success"
});
}
},
error: function (response) {
$.notify(response.message, {
globalPosition: "top center",
className: "success"
});
}
});
});
我总是收到这个enter image description here
我希望被重定向到主页/新闻列表网址。我尝试更改 ajax call 的所有参数,添加和删除它们,但无论我做什么,我总是登陆这个页面。
【问题讨论】:
-
试试
window.location.replace(response.redirectToUrl)而不是window.location.href = response.redirectToUrl; -
还是一样的,调用一结束,就到了这个页面,成功回调方法也没有触发。我在哪里设置这个值。
-
有趣!你能放下“窗户”吗?看看会发生什么
-
error回调是否正在执行? -
您是如何测试“从未调用成功回调”的? if语句前的'console.log(response)'能成功吗?
标签: javascript c# asp.net ajax asp.net-mvc-5