![](/img/trans.png)
[英]Why am I getting a json parse error during an ajax call after updating to Codeigniter 3.x?
[英]Why am I getting json response page after ajax post call?
我有一个 aps.net MVC5 web 应用程序,我有一个控制器操作 post 方法,看起来像这样,
[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"
});
}
});
});
我总是在这里输入图像描述
我希望被重定向到 home/newslist url。 我尝试更改 ajax call 的所有参数,添加和删除它们,仍然不管我做什么,我总是登陆这个页面。
尝试从 ajax 中删除“错误”功能后进行检查。 你也可以试试这个:
$(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 (jqXHR, textStatus, errorThrown) {
$.notify(errorThrown, {
globalPosition: "top center",
className: "success"
});
}
});
});
检查成功函数是否正在调用。
如果您需要在 ajax 响应中返回完整的 HTML 页面,那么您需要通过一些更改来更改 Controller 方法。
[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);
return view("ViewName");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.