EasyUI form ajax submit到MVC后,在IE下提示下载内容的解决办法

时间:2023-03-08 21:27:29

问题描述:

  项目环境为,.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方法:
 /// <summary>
/// 自定义JsonResult,
/// </summary>
/// <param name="data">数据</param>
/// <param name="behavior">行为</param>
/// <returns>JsonReuslt</returns>
protected JsonResult JsonSubmit(object data, JsonRequestBehavior behavior = JsonRequestBehavior.AllowGet)
{
return new JsonResultOverride
{
Data = data,
ContentType = "text/html",
//ContentEncoding = contentEncoding,
JsonRequestBehavior = behavior,
FormateStr = "yyyy-MM-dd HH:mm:ss"
};
}

附JsonResultOverride类:

 /*****************************************************************************************************
* 本代码版权归Quber所有,All Rights Reserved (C) 2015-2088
* 联系人邮箱:qubernet@163.com
*****************************************************************************************************
* 命名空间:QUBER.Web.App_Common.Mvc
* 类名称:JsonResultOverride
* 创建时间:2015-11-25 15:53:04
* 创建人:Quber
* 创建说明:重写MVC的JsonResult
*****************************************************************************************************
* 修改人:
* 修改时间:
* 修改说明:
*****************************************************************************************************/
using System;
using System.IO;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters; namespace QUBER.Web.App_Common.Mvc
{
/// <summary>
/// 重写MVC的JsonResult
/// </summary>
public class JsonResultOverride : JsonResult
{
#region 属性
/// <summary>
/// 格式化字符串
/// </summary>
public string FormateStr { get; set; } /// <summary>
/// Newtonsoft.Json序列化配置
/// </summary>
public JsonSerializerSettings Settings { get; private set; }
#endregion #region 构造
/// <summary>
/// 构造方法
/// </summary>
public JsonResultOverride()
{
Settings = new JsonSerializerSettings
{
//解决.Net MVC EntityFramework Json 序列化循环引用问题
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
};
//添加默认时间转换格式
//Settings.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });
}
#endregion #region 重写
/// <summary>
/// 重写执行视图
/// </summary>
/// <param name="context">上下文</param>
public override void ExecuteResult(ControllerContext context)
{
if (context == null) { throw new ArgumentNullException("context"); }
if (JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException("JSON GET is not allowed"); }
var response = context.HttpContext.Response;
response.ContentType = string.IsNullOrEmpty(ContentType) ? "application/json" : ContentType;
if (ContentEncoding != null) { response.ContentEncoding = ContentEncoding; }
if (Data == null) { return; }
var scriptSerializer = JsonSerializer.Create(Settings);
scriptSerializer.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = FormateStr });
using (var sw = new StringWriter())
{
scriptSerializer.Serialize(sw, Data);
response.Write(sw.ToString());
}
}
#endregion #region 方法 #endregion
}
}