0

So there are lots of questions out there about this whole "IE opens a download file dialog for Json data returned via Ajax" (like this one or this one), but I have not found any answers for those who use Unobtrusive Ajax.

We do a simple Ajax.BeginForm like this:

Ajax.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, 
    new AjaxOptions { UpdateTargetId = "ContentContainer", OnSuccess = "JsonCheckRedirect" }, 
    new { id = "form1" }
)

This posts the form with Content Type of "application/x-www-form-urlencoded; charset=UTF-8". The Controller processes the form, and if there is an error, it returns a PartialView with Content Type "text/html", and that HTML gets put into the ContentContainer (because of the "UpdateTargetId" property in the AjaxOptions).

If there is no error, and the user logged in successfully, it returns Json with the URL they should be redirected to. So in the Controller, it's just simply this:

return Json(new { Url = returnUrl });

Except that we just had a user notify us that in IE8, this is causing the download dialog to pop up. After reading all these other questions with the same issue, I understand why this is happening, but the fix for those situations is to use this Json return instead:

return Json(new { Url = returnUrl }, "text/html");

The problem is that when I return like this, I guess Unobtrusive Ajax sees that the response from the server is HTML, and it replaces the content in the UpdateTargetId container. It does not do this when the server responds with "application/json".

Is there any way around this?

(do I need to explain anything further?)

Community
  • 1
  • 1
John Washam
  • 4,073
  • 4
  • 32
  • 43
  • Is it possible to remove the `UpdateTargetId` from the options and just handle updating the content yourself with the `OnSuccess` and `OnFailure` options? I'm not sure what arguments are passed to these handlers or if omitting the `UpdateTargetId` option will cause an error or cause the ajax form not to work. – asymptoticFault Sep 09 '13 at 22:43

1 Answers1

0

If you return the url like that, you are going to need to override the Success handler and check the result to see if it has the Url property and then putting the result into window.location.

Personally, rather than returning a new Json result, I would send back a 302 which should make the ajax handler redirect you.

This happens in the bowels of jQuery if memory serves, so you don't need to change anything.

So rather than returning that json result on a successful login, try using a redirectToRoute result instead and send them to the page you want them to go to.

Slicksim
  • 7,054
  • 28
  • 32
  • Sadly, there's a problem with that implementation. If you have an Ajax-submitted form and the AjaxOptions "UpdateTargetId" is set, when you send back a 302 redirect, it will load that redirect URL into the UpdateTargetId element (according to http://stackoverflow.com/questions/1538523/how-to-get-an-asp-net-mvc-ajax-response-to-redirect-to-new-page-instead-of-inser). But on this same question, I did find that you can return `JavascriptResult`, so I'll try that and see what happens. Thanks for the answer! – John Washam Sep 10 '13 at 13:40