0

I have a MVC action that can either return a RedirectToAction (302) or a partial view. I'm using Ajax.BeginForm and an OnSuccess handler to capture the response. My problem is, whenever a redirect response is issued, it seems something (not sure if it's a native AJAX behavior or not) is handling the redirect and returning the redirected page, INSTEAD of the response containing the 302 headers. It's essentially bypassing the 302 response and automatically retrieving the redirected page. This isn't what I want.

My goal, with the response, was to look at the headers in the OnSuccess event and gracefully handle either a redirect or a partial view update of the current page.

Is there any way to return a "raw" 302 response? Thanks.

Ryan Peters
  • 7,608
  • 8
  • 41
  • 57

1 Answers1

0

There is no way to make JavaScript AJAX queries to not follow redirects. (See How to prevent ajax requests to follow redirects using jQuery for more discussion).

You should consider other options - i.e. returning JSON with "redirect location" would be more AJAX-friendly.

 return new JsonResult { 
     JsonRequestBehavior = JsonRequestBehavior.AllowGet,
     Data = new { Location = Url.Action("ActionName") }};
Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • I realized that the AJAX spec always follows the URL for a redirect response after I posted this. A similar version of your result was what I ended up coding. Thanks. – Ryan Peters Jun 15 '15 at 16:15