IE prompts to open or save json result from server

后端 未结 9 1241
既然无缘
既然无缘 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: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

提交回复
热议问题