Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么在ajax post调用之后我会收到json响应页面?_Javascript_C#_Asp.net_Ajax_Asp.net Mvc 5 - Fatal编程技术网

Javascript 为什么在ajax post调用之后我会收到json响应页面?

Javascript 为什么在ajax post调用之后我会收到json响应页面?,javascript,c#,asp.net,ajax,asp.net-mvc-5,Javascript,C#,Asp.net,Ajax,Asp.net Mvc 5,我有一个aps.net MVC5 web应用程序,我有一个控制器操作post方法,看起来像这样 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Set(int id) { DbHelper dbHelper = new DbHelper(); dbHelper.Update<News>("update ne

我有一个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);
        }
我总是得到这个


我希望被重定向到主页/新闻列表url。我尝试更改ajax调用的所有参数,添加和删除它们,但无论我做什么,我始终会在这个页面上登录。

尝试在从ajax中删除“error”函数后进行检查。您也可以尝试以下方法:

$(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"
            });
        }
    });
});

检查是否调用了success函数。

从ajax中删除“error”函数后,尝试进行检查。您也可以尝试以下方法:

$(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"
            });
        }
    });
});

检查是否调用了success函数。

如果需要在ajax响应中返回完整的HTML页面,则需要更改控制器方法

[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");
}
[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果集(int id)
{
DbHelper DbHelper=新的DbHelper();
Update(“更新新闻集ispublished=1,publishdate=@publishdate,其中newsid=@newsid”,new{newsid=id,publishdate=DateTime.Now}”);
//返回Json(新的{success=true,message=“added successfully!”,redirectToUrl=Url.Action(“NewsList”,“Home”)},JsonRequestBehavior.AllowGet);
返回视图(“视图名称”);
}

如果需要在ajax响应中返回完整的HTML页面,则需要通过一些更改来更改控制器方法

[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");
}
[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果集(int id)
{
DbHelper DbHelper=新的DbHelper();
Update(“更新新闻集ispublished=1,publishdate=@publishdate,其中newsid=@newsid”,new{newsid=id,publishdate=DateTime.Now}”);
//返回Json(新的{success=true,message=“added successfully!”,redirectToUrl=Url.Action(“NewsList”,“Home”)},JsonRequestBehavior.AllowGet);
返回视图(“视图名称”);
}


尝试
window.location.replace(response.redirectToUrl)
而不是
window.location.href=response.redirectToUrl还是一样,一旦调用完成,它就会到达这个页面,甚至不会触发成功回调方法。我正在设置此值的位置。intresting!你能放下“窗口”看看执行的
错误
回调发生了什么吗?你是如何测试“从不调用成功回调”的?能否在if语句成功之前“console.log(response)”?请尝试
window.location.replace(response.redirectToUrl)
而不是
window.location.href=response.redirectToUrl还是一样,一旦调用完成,它就会到达这个页面,甚至不会触发成功回调方法。我正在设置此值的位置。intresting!你能放下“窗口”看看执行的
错误
回调发生了什么吗?你是如何测试“从不调用成功回调”的?你能在if语句成功之前“console.log(response)”吗?这没有任何区别。你检查过后端方法
ActionResult Set(int-id)
是否在ajax请求后被命中了吗?还要确保html中的“.set”按钮不能是提交类型。是的,它被点击了,这就是返回json响应的原因。还要确保“.set”html中的按钮不能是Submit类型。我更改了按钮类型,但仍然得到相同的响应。这没有任何区别。您是否检查了后端方法
ActionResult Set(int id)
是否在ajax请求后被命中?还要确保html中的“.set”按钮不能是提交类型。是的,它被点击了,这就是返回json响应的原因。还要确保“.set”html中的按钮不能是提交类型。我更改了按钮类型,但仍然得到相同的响应。它不会再次重新加载整个页面吗?然后需要返回json响应是最佳选项或部分视图。它不会再次加载整个页面吗?然后需要返回json响应是最佳选项或部分视图。