IE9 JSON Data “do you want to open or save this file”

后端 未结 5 545
温柔的废话
温柔的废话 2020-11-28 08:57

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

相关标签:
5条回答
  • 2020-11-28 09:35

    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);
    });
    
    0 讨论(0)
  • 2020-11-28 09:42

    (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

    0 讨论(0)
  • 2020-11-28 09:46

    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!

    0 讨论(0)
  • 2020-11-28 09:46

    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".

    0 讨论(0)
  • 2020-11-28 09:49

    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
    
    0 讨论(0)
提交回复
热议问题