After EasyUI form ajax submit to MVC, the system prompts the solution for downloading content in IE, easyuimvc

Source: Internet
Author: User

After EasyUI form ajax submit to MVC, the system prompts the solution for downloading content in IE, easyuimvc

Problem description:

The project environment is. Net Mvc5 + EF6 ...... The front-end framework uses EasyUI v1.4.4. On the view page, when the form data is submitted using the form submit method, if IE is used, IE will prompt the download content after the request is successful, this is because the default Content-Type of IE is text/html, and the default Content-Type of Google browser we like to use is application/json. Solution:Based on the Problem description above, we can set the returned Content-Type to text/html before returning data. Solution code:We can customize a JsonResult method: 1 /// <summary> 2 /// custom JsonResult, 3 /// </summary> 4 /// <param name = "data"> data </param> 5 /// <param name = "behavior"> 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

Attached JsonResultOverride class:

1 /************************************** **************************************** * *********************** 2 * This code is copyrighted by Quber, all Rights Reserved (C) 2015-2088 3 * contact email: 4 ************************************** **************************************** * *********************** 5 * namespace: QUBER. web. app_Common.Mvc 6 * Class Name: JsonResultOverride 7 * Creation Time: 15:53:04 8 * created by: Quber 9 * creation instructions: Rewrite JsonResult10 ************************************** of MVC ************************************ ****************************************: 12 * modification time: 13 * modification description: 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 // rewrite JsonResult25 of MVC /// </summary> 26 public class JsonResultOverride: jsonResult27 {28 # region attribute 29 // <summary> 30 // Format string 31 /// </summary> 32 public string FormateStr {get; set ;} 33 34 /// <summary> 35 /// Newtonsoft. json serialization configuration 36 // </summary> 37 public JsonSerializerSettings Settings {get; private set;} 38 # endregion39 40 # region Construction 41 // <summary> 42/ // Constructor 43 // </summary> 44 public JsonResultOverride () 45 {46 Settings = new JsonSerializerSettings47 {48 // solution. net MVC EntityFramework Json serialization cycle reference problem 49 ReferenceLoopHandling = ReferenceLoopHandling. ignore, 50}; 51 // Add default time conversion format 52 // Settings. converters. add (new IsoDateTimeConverter {DateTimeFormat = "yyyy-MM-dd HH: mm: ss "}); 53} 54 # endregion55 56 # region rewrite 57 // <summary> 58 // rewrite execution view 59 /// </summary> 60 // <param name = "context"> 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 allowe D ");} 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 method 80 81 # endregion82} 83}View Code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.