IE prompts to open or save json result from server

后端 未结 9 1202
既然无缘
既然无缘 2020-11-29 22:28

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

相关标签:
9条回答
  • 2020-11-29 22:31

    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/

    0 讨论(0)
  • 2020-11-29 22:38

    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.

    0 讨论(0)
  • 2020-11-29 22:38

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

    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.

    0 讨论(0)
  • 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'

    0 讨论(0)
  • 2020-11-29 22:41

    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.

    0 讨论(0)
  • 2020-11-29 22:42

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