问题
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