IE9 JSON Data "do you want to open or save this file"
Asked Answered
O

5

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

Onetime answered 22/3, 2011 at 9:7 Comment(5)
what are the HTTP headers being sent back with the JSON response from the server?Edwards
Have you tried the same page in a different browser such as FF? Have you got javascript enabled in IE 9?Dabney
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).Allopath
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 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">. I also added a meta tag for <meta http-equiv="X-UA-Compatible" content="IE=100">Onetime
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";Onetime
O
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
Onetime answered 30/3, 2011 at 0:49 Comment(1)
See my answer below, it should help: #5389393Salts
T
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!

Teletype answered 8/5, 2012 at 16:14 Comment(2)
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?Lombok
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!Coriander
S
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

Salts answered 9/4, 2013 at 19:39 Comment(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.Organogenesis
G
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);
});
Graubert answered 7/9, 2012 at 9:54 Comment(3)
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).Winding
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<System.Net.Http.HttpResponseMessage>Distortion
@Vipul: I think there was something wrong with your Task method, could you copy your code here?Graubert
T
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".

Top answered 1/3, 2013 at 1:37 Comment(1)
Actually IE8 is also broken and needs the change.Kerosene
O
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
Onetime answered 30/3, 2011 at 0:49 Comment(1)
See my answer below, it should help: #5389393Salts

© 2022 - 2024 — McMap. All rights reserved.