Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 748
xaisoft Avatar asked Nov 15 '13 21:11

xaisoft


1 Answers

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;     } }); 
like image 126
Tommy Avatar answered Oct 04 '22 06:10

Tommy