RedirectToAction not working after successful jquery ajax post? [duplicate]

会有一股神秘感。 提交于 2019-11-26 19:34:54

问题


The following does not redirect my page: Here is the MVC code:

    [HttpPost]
    public ActionResult GoHome()
    { 
         return RedirectToAction("Index", "Home");   
    }

Here is the ajax post:

   $.support.cors = true;

            $.ajax({
                type: "POST",
                url: "http://localhost/UserAccount/GoHome",
                dataType: 'json',
                crossDomain: true
            });

The post is successful and when it hists the GoHome action it does not redirect to the Index Action of the Home Controller.


回答1:


You cannot redirect from an AJAX post. You could return the URL you want to redirect the browser to however and redirect from Javascript.

Controller

[HttpPost]
public ActionResult GoHome()
{ 
     return Json(Url.Action("Index", "Home"));   
}

Javascript

$.ajax({
    type: "POST",
    url: "http://localhost/UserAccount/GoHome",
    dataType: 'json',
    crossDomain: true,
    success: function (data) {
        window.location.href = data;
    }
});


来源:https://stackoverflow.com/questions/20011282/redirecttoaction-not-working-after-successful-jquery-ajax-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!