问题描述:
项目环境为,.Net Mvc5+EF6……
前端框架使用的是EasyUI v1.4.4。
在视图页面中,使用form的submit方法提交表单数据时,如果是使用IE的话,请求成功后IE会提示下载内容,关于这个下载内容,则是由于IE默认的Content-Type为text/html,而我们喜欢用的Google浏览器默认的Content-Type为application/json。
解决办法: 那么根据以上的问题描述,我们可以在返回数据之前设置返回的Content-Type为text/html即可。
解决代码: 我们可以自定义一个JsonResult方法:
1 /// <summary> 2 /// 自定义JsonResult, 3 /// </summary> 4 /// <param name="data">数据</param> 5 /// <param name="behavior">行为</param> 6 /// <returns>JsonReuslt</returns> 7 protected JsonResult JsonSubmit(object data, JsonRequestBehavior behavior = JsonRequestBehavior.AllowGet) 8 { 9 return new JsonResultOverride10 {11 Data = data,12 ContentType = "text/html",13 //ContentEncoding = contentEncoding,14 JsonRequestBehavior = behavior,15 FormateStr = "yyyy-MM-dd HH:mm:ss"16 };17 }
View Code
附JsonResultOverride类:
1 /***************************************************************************************************** 2 * 本代码版权归Quber所有,All Rights Reserved (C) 2015-2088 3 * 联系人邮箱:qubernet@163.com 4 ***************************************************************************************************** 5 * 命名空间:QUBER.Web.App_Common.Mvc 6 * 类名称:JsonResultOverride 7 * 创建时间:2015-11-25 15:53:04 8 * 创建人:Quber 9 * 创建说明:重写MVC的JsonResult10 *****************************************************************************************************11 * 修改人:12 * 修改时间:13 * 修改说明:14 *****************************************************************************************************/15 using System;16 using System.IO;17 using System.Web.Mvc;18 using Newtonsoft.Json;19 using Newtonsoft.Json.Converters;20 21 namespace QUBER.Web.App_Common.Mvc22 {23 /// <summary>24 /// 重写MVC的JsonResult25 /// </summary>26 public class JsonResultOverride : JsonResult27 {28 #region 属性29 /// <summary>30 /// 格式化字符串31 /// </summary>32 public string FormateStr { get; set; }33 34 /// <summary>35 /// Newtonsoft.Json序列化配置36 /// </summary>37 public JsonSerializerSettings Settings { get; private set; }38 #endregion39 40 #region 构造41 /// <summary>42 /// 构造方法43 /// </summary>44 public JsonResultOverride()45 {46 Settings = new JsonSerializerSettings47 {48 //解决.Net MVC EntityFramework Json 序列化循环引用问题49 ReferenceLoopHandling = ReferenceLoopHandling.Ignore,50 };51 //添加默认时间转换格式52 //Settings.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });53 }54 #endregion55 56 #region 重写57 /// <summary>58 /// 重写执行视图59 /// </summary>60 /// <param name="context">上下文</param>61 public override void ExecuteResult(ControllerContext context)62 {63 if (context == null) { throw new ArgumentNullException("context"); }64 if (JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException("JSON GET is not allowed"); }65 var response = context.HttpContext.Response;66 response.ContentType = string.IsNullOrEmpty(ContentType) ? "application/json" : ContentType;67 if (ContentEncoding != null) { response.ContentEncoding = ContentEncoding; }68 if (Data == null) { return; }69 var scriptSerializer = JsonSerializer.Create(Settings);70 scriptSerializer.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = FormateStr });71 using (var sw = new StringWriter())72 {73 scriptSerializer.Serialize(sw, Data);74 response.Write(sw.ToString());75 }76 }77 #endregion78 79 #region 方法80 81 #endregion82 }83 }
View Code
原标题:EasyUI form ajax submit到MVC后,在IE下提示下载内容的解决办法
关键词:ajax