I’m returning some Json via a C# MVC Controller. Other browsers work fine, but Internet Explorer (IE9) tries to Save the returned Json data as a Text file. Any ideas how to keep this from happening?
//MVC Controller Code...
return Json(new { redirectToUrl = Url.Action("Create", "Album",
new { url = url, isLocalFile = isLocalFile})});
//Clientside Javascript Code
$("#uploadImageForm").ajaxForm({
beforeSubmit: function () {
},
success: function (data, textStatus, xhr) {
window.location.href = data.redirectToUrl;
},
error: function (data, textStatus, xhr) {
}
});
I’ve tried adding “text/plain” and “text/json” to the second argument of the return Json method, it doesn’t work.
Many thanks!
Quote from the documentation of the
jquery.formplugin:This means that if your form contains file input fields and you are submitting this form to a controller action that returns JSON, you must wrap this JSON in a
<textarea>tags.So your response should not look like this:
it should look like this:
In order to achieve that you could use a custom action result that will wrap the response with those tags:
and then have your controller action return this custom result:
Now obviously in your AJAX success callback you also need to account for this difference by testing the
typeof resultand in the case of a legacy browser (such as Internet Explorer) manually parse the response. You may take a look at the source code of the page I have linked to.But this being said, I can see that in your success callback you are redirecting to a controller action contained in the JSON response returned by the server. Here comes my question: What’s the point of using AJAX in the first place if you are going to redirect? Why don’t you use a standard form post to the controller action and have the controller action directly perform the redirect? AJAX should be used when you want to stay on the same page.