56

Internet explorer in compatibility mode gets the data from the server in an ajax callback method, and pops-up a dialog if I want to save the data or open. How to get rid of that?

client says:

$.ajax({
        type:'POST',
        data: $("#UIdlgHolder > form").serialize(),
        url: $("#UIdlgHolder > form").attr("action"),
        success: function (data, textStatus, jqXHR) {
            {
                alert(data.message);
            }
}

server answers:

return new JsonResult { Data = new { result = false, message = "Yay!" } };
andrewtweber
  • 24,520
  • 22
  • 88
  • 110
iLemming
  • 34,477
  • 60
  • 195
  • 309

9 Answers9

36

Even though it's not supposedly the correct way, setting the content type to text/html made IE deal with this correctly for me:

return Json(result, "text/html");

Works in all the version that F12 tools gives you in IE9.

ItsJason
  • 747
  • 1
  • 4
  • 20
  • 9
    It's not the correct way. There is no "supposedly" about it. – Cheeso Apr 30 '12 at 20:57
  • 2
    This is the only thing that worked for me. I had no control over the ajax request (third-party control) and IE9 would *not* work. So even if it isn't the correct way, it still works (with no problems on other browsers so far) – kkara Aug 28 '12 at 12:43
  • Its annoying, but correct. I had a form that i was capturing the Submit() event on and firing AJAX to get a JSON response, and IE kept stupidly trying to Open/Save the JSON response in the background! This cleared it up, at the cost of loosing the nice JSON formatting in Firefox when viewing that URL directly. – Graham Mar 07 '13 at 20:50
  • 2
    The reason this is wrong is that angle brackets, which are valid within JSON, result in the browser treating the JSON as mal-formed HTML – Dexygen Dec 15 '14 at 18:45
  • Thanks alot. After wasting 6 hrs i just did this. my bluimp file upload working awesome now on IE9 too. Thanks again. – Zohaib Aug 27 '15 at 15:05
  • 1
    Bit late to the party, but one more note - this appears to only be an issue in IE9 (and below) when the form is multipart/form-data. So you can avoid setting incorrect headers except in that circumstance. – Colin Sep 02 '15 at 09:38
  • @GeorgeJempty is correct. A better alternative would be `"text/plain"`. – gilly3 Jan 15 '16 at 17:13
32

If using MVC, one way of handling this is to implement a base controller in which you override (hide) the Json(object) method as follows:

public class ExtendedController : Controller
{
    protected new JsonResult Json(object data)
    {
        if (!Request.AcceptTypes.Contains("application/json"))
            return base.Json(data, "text/plain");
        else
            return base.Json(data);
    }
}

Now, your controllers can all inherit ExtendedController and simply call return Json(model); ...

  • without modifying the response content type for those browsers which play nicely (not <=IE9 !)
  • without having to remember to use Json(data, "text/plain") in your various Ajax action methods

This works with json requests which would otherwise display the "Open or Save" message in IE8 & IE9 such as those made by jQuery File Upload

Chris
  • 3,210
  • 1
  • 33
  • 35
14

Sounds like this SO question may be relevant to you:

How can I convince IE to simply display Application json rather than offer to download

If not:

Have you tried setting the dataType expected in the ajax options? i.e. dataType: 'json'

Have you tried other content types such as 'application/json' or 'text/javascript'

Community
  • 1
  • 1
Cargowire
  • 1,488
  • 10
  • 21
2

I changed the content-type to "text/html" instead of "application/json" server side before returning the response. Described it in a blog post, where other solutions have also been added:

http://blog.degree.no/2012/09/jquery-json-ie8ie9-treats-response-as-downloadable-file/

Andreas
  • 705
  • 10
  • 22
2

Sadly, this is just another annoying quirk of using Internet Explorer.

enter image description here

The simple solution is to run a small .reg file on your PC, to tell IE to automatically open .json files, rather than nag about whether to open/save it.

I've put a copy of the file you'll need here:

JSON mime type

You'll need to have Admin rights to run this.

Community
  • 1
  • 1
Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
  • 1
    (Why did 2 people vote down this answer...? You *do* need to install a small registry change to get IE to automatically open JSON data, when you call a service from jQuery, rather than asking to save it, and no one else offered this suggestion.... I thought my answer was really useful. I lose faith in StackOverflow sometimes....) – Mike Gledhill Feb 23 '18 at 07:42
2

Have you tried to send your ajax request using POST method ? You could also try to set content type to 'text/x-json' while returning result from the server.

1

I faced this while using jQuery FileUpload plugin.

Then I took a look in their documentation, most exactly in the Content-Type Negotiation section and followed their suggestion for Ruby/Rails.

render(json: <some-data>, content_type: request.format)

Which fixed the issue for me.

Quick Explanation: for old IE/Opera versions, this plugin will use an iframe with text/plain or text/html content-type, so if you force the response to json, browser will try download it. Using the same content-type as in the request will make it work for any browser.

Rael Gugelmin Cunha
  • 3,327
  • 30
  • 25
1

Is above javascript code the one you're using in your web application ? If so - i would like to point few errors in it: firstly - it has an additional '{' sign in definition of 'success' callback function secondly - it has no ')' sign after definition of ajax callback. Valid code should look like:

$.ajax({
        type:'POST',
        data: 'args',
        url: '@Url.Action("PostBack")',
        success: function (data, textStatus, jqXHR) {
                alert(data.message);
            }
    });

try using above code - it gave me 'Yay' alert on all 3 IE versions ( 7,8,9 ).

0

In my case, IE11 seems to behave that way when there is some JS syntax error in the console (doesn't matter where exactly) and dataType: 'json' has no effect at all.

Artur INTECH
  • 6,024
  • 2
  • 37
  • 34