38

Started testing my jQuery applications with IE9. Looks like I may be in for some trouble here. I noticed that when I return JSON data back to the Javascript methods I always get this Prompt that says: "Do you want to open or save this file?" and provides me with 3 buttons: Open, Save and Cancel. Of course, my javascript is taking actions based on the values set in the JSON object but since IE9 doesn't pass it over to the script, I cannot execute the follow up action from there on.

Anyone else facing this issue? Here is a snapshot.enter image description here

Anup Marwadi
  • 2,517
  • 4
  • 25
  • 42
  • 3
    what are the HTTP headers being sent back with the JSON response from the server? – BigFatBaby Mar 22 '11 at 09:08
  • Have you tried the same page in a different browser such as FF? Have you got javascript enabled in IE 9? – Xhalent Mar 22 '11 at 09:10
  • You're almost certainly sending the wrong MIME Content-Type, or doing something very odd like sending a JSON response back to an IFRAME tag. A repro URL will allow us to easily see. (Enabling vs. disabling JS would make no difference at all). – EricLaw Mar 22 '11 at 13:32
  • sorry for the delay in response. I am setting the dataType to "text json". If I just set it to "json", it thinks it has to do a jsonp and returns it back with a Callback. This is common across all browsers. However, my code works on IE8, FF4.0 and the new Chrome(10?). I read elsewhere that this might have to do with the . I also added a meta tag for – Anup Marwadi Mar 26 '11 at 23:04
  • Actually, you were right @EricLaw. After setting the content type in the Json result, it worked. I had to add the following lines: result.ContentEncoding = System.Text.Encoding.UTF8; result.ContentType = "application/json; charset=UTF-8"; – Anup Marwadi Mar 26 '11 at 23:17

5 Answers5

21

If anyone is using ASP.net MVC and trying to fix this issue - I used the following built in methods in the MVC framework. Simply update the content Type and encoding on the JsonResult.

public ActionResult Index(int id)
{
        // Fetch some data
        var someData = GetSomeData();

        // Return and update content type and encoding
        return Json(someData, "text/html", System.Text.Encoding.UTF8,
                        JsonRequestBehavior.AllowGet);
}

This fixed the issue for me!

Deano
  • 2,805
  • 3
  • 29
  • 42
  • I see the `Json` class is in `System.Web.Helpers`, but I'm getting a `"Method, delegate, or event expected"` message and it will not build. Know by chance what I'm missing? – atconway Jul 30 '13 at 21:20
  • 1
    With this approach the developer has to remember to set the content-type to "text/html" everytime they are returning JSON. What if they forget? Not a good solution! Plus, returning JSON data and telling the browser it is HTML content is fundamentally wrong! – Mosh Dec 04 '13 at 01:48
13

(Answer originally posted for this question.)

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

Community
  • 1
  • 1
Chris
  • 3,210
  • 1
  • 33
  • 35
  • 1
    I used "text/html" instead of "text/plain", but that could be caused by the specific implementation I used on the client. Just mentioning it for someone who has the same problem. – Robin van der Knaap May 20 '13 at 22:47
6

I also faced this problem yesterday with WebAPI which returned a list of URLs (of asynchronously uploaded files).

Just set content type to "text/html" instead of default "application/json; charset=UTF-8" of WebAPI services. I got response as a JSON string and then used $.parseJSON to convert it to JSON object.

public async Task<HttpResponseMessage> Upload()
{
  // ...
  var response = Request.CreateResponse(HttpStatusCode.OK, files);
  response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
  return response;
}

// result is an iframe's body content that received response.
$.each($.parseJSON(result.html()), function (i, item)
{
  console.log(item.Url);
});
Tien Do
  • 10,319
  • 6
  • 41
  • 42
  • Thanks for the response regarding webapi. I have this exact problem and setting the ContentType worked. I tried setting "text/html" through the CreateResponse overload, but MVC throw an exception stating it can't find a formatter for that (only supports json and xml). – John Livermore Nov 12 '12 at 21:37
  • I am also facing same problem, I tried this solution but it is generating conversion error: Cannot implicitly convert type 'System.Net.Http.HttpResponseMessage' to 'System.Threading.Tasks.Task – Vipul Nov 27 '12 at 05:43
  • @Vipul: I think there was something wrong with your Task method, could you copy your code here? – Tien Do Nov 28 '12 at 09:55
3

In my case when contentType in response header is "application/json; charset=UTF-8", the IE 9 shows that Prompt. But changed to "text/html" then the prompt does not show, although all otter browsers are fine with the "application/json; charset=UTF-8".

Rich
  • 31
  • 1
2

Actually, you were right @EricLaw. After setting the content type in the Json result, it worked. I had to add the following lines:

 result.ContentEncoding = System.Text.Encoding.UTF8; 
 result.ContentType = "application/json; charset=UTF-8
Anup Marwadi
  • 2,517
  • 4
  • 25
  • 42
  • See my answer below, it should help: http://stackoverflow.com/questions/5388893/ie9-json-data-do-you-want-to-open-or-save-this-file/15911008#15911008 – Chris Apr 25 '13 at 13:19