RedirectToAction not working after successful jque

2019-01-21 21:59发布

This question already has an answer here:

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条回答
疯言疯语
2楼-- · 2019-01-21 22:54

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;
            }
        });
查看更多
登录 后发表回答