Redirect to page after post from inside ajax

I want to redirect to an Index, Admin Admin action after a message from ajax.

$.post("/Admin/Create", { inputs: inputs, columnsCount: columnsCount, }); 

How can I change this code to redirect it to the index page after success?

+4
source share
3 answers

use the third post parameter

 $.post( "/Admin/Create", { inputs: inputs, columnsCount: columnsCount, }, function() { window.location.replace("/Admin/index"); } ); 
+10
source

While window.location.replace or window.location.href definitely work, I recommend that you take a step back and consider whether you need an Ajax call at all!

Ajax is used to refresh part of the page, and if you partially refresh the page, you will not redirect the user to another page.

So, if you think carefully, you can replace your ajax post with regular mail and redirect the user to another action from the controller itself. This way, you will also follow a much cleaner Post-Redirect-Get pattern.

+4
source
 $.post("/Admin/Create", { inputs: inputs, columnsCount: columnsCout }, function(){ window.location.href = "http://..../Admin/Index"; }); 
0
source

Source: https://habr.com/ru/post/1301695/


All Articles