I m not satisfied by the best answer by the Joseph, instead of fixing the correct problem, he told that this is wrong use case. In fact there are many places for example if you are converting an old codebase to ajaxified code and there you NEED it, then you NEED it. In programming there is no excuse because its not only you who is coding its all bad and good developers and you have to work side by side. So if I don't code redirection in ajax my fellow developer can force me to have a solution for it. Just like I like to use all AMD patterned sites or mvc4, and my company can keep me away from it for a year.
So let's talk on the solution now.
I have done hell heck of ajax request and response handling and the simplest way I found out was to send status codes to the client and have one standard javascript function to understand those codes. If i simply send for example code 13 it might meant a redirect.
So a json response like { statusCode: 13, messsage: '/home/logged-in' }
of course there are tons of variations proposed like
{ status: 'success', code: 13, url: '/home/logged-in', message: 'You are logged in now' }
etc , so up to your own choice of standard messages
Usually I Inherit from base Controller class and put my choice of standard responses like this
public JsonResult JsonDataResult(object data, string optionalMessage = "")
{
return Json(new { data = data, status = "success", message = optionalMessage }, JsonRequestBehavior.AllowGet);
}
public JsonResult JsonSuccessResult(string message)
{
return Json(new { data = "", status = "success", message = message }, JsonRequestBehavior.AllowGet);
}
public JsonResult JsonErrorResult(string message)
{
return Json(new { data = "", status = "error", message = message }, JsonRequestBehavior.AllowGet);
}
public JsonResult JsonRawResult(object data)
{
return Json(data, JsonRequestBehavior.AllowGet);
}
About using $.ajax intead of Ajax.BeginForm
I would love to use Jquery ajax and I do, but again its not me in the whole world to make decisions
I have an application full of Ajax.BeginForm and of course I didnt do that. But i have to live with it.
So There is a success callback in begin form too, you don't need to use jquery ajax to use callbacks
Something about it here
Ajax.BeginForm, Calls Action, Returns JSON, How do I access JSON object in my OnSuccess JS Function?
Thanks