在IE下使用EasyUI form外掛程式建立或編輯資料時(操作成功後會返回一段json),始終無法運行回呼函數,而是提示下載內容。
在IE9下的提示如:為瞭解決這個問題,需要將json字串用下面的格式返回給用戶端才行。
<body>
<pre>{"message":"儲存成功","data":null,"success":true}</pre>
</body>
所以寫了一個hack方法:
View Code 1 /// <summary>
2 /// 前台Ajax請求的統一返回結果類
3 /// </summary>
4 public class AjaxResult
5 {
6 private AjaxResult()
7 {
8 }
9
10 private bool _isError = false;
11
12 /// <summary>
13 /// 是否產生錯誤
14 /// </summary>
15 public bool IsError { get { return _isError; } }
16
17 /// <summary>
18 /// 錯誤資訊,或者成功資訊
19 /// </summary>
20 public string Message { get; set; }
21
22 /// <summary>
23 /// 成功或失敗時返回的資料
24 /// </summary>
25 public object Data { get; set; }
26
27 /// <summary>
28 /// 指示前台應該做什麼操作
29 /// </summary>
30 public string Action { get; set; }
31
32 #region Error
33 public static AjaxResult Error()
34 {
35 return new AjaxResult()
36 {
37 _isError = true
38 };
39 }
40 public static AjaxResult Error(string message)
41 {
42 return new AjaxResult()
43 {
44 _isError = true,
45 Message = message
46 };
47 }
48 #endregion
49
50 #region Success
51 public static AjaxResult Success()
52 {
53 return new AjaxResult()
54 {
55 _isError = false
56 };
57 }
58 public static AjaxResult Success(string message)
59 {
60 return new AjaxResult()
61 {
62 _isError = false,
63 Message = message
64 };
65 }
66 public static AjaxResult Success(object data)
67 {
68 return new AjaxResult()
69 {
70 _isError = false,
71 Data = data
72 };
73 }
74 public static AjaxResult Success(string message, object data)
75 {
76 return new AjaxResult()
77 {
78 _isError = false,
79 Data = data,
80 Message = message
81 };
82 }
83 #endregion
84
85 public override string ToString()
86 {
87 return new JavaScriptSerializer().Serialize(this);
88 }
89
90 /*When using form ajax submit, the server response should be an HTML file with a textarea element or a pre element. The response data should be inside the textarea element or pre element. For example:
91 <body>
92 <pre>{"message":"儲存成功","data":null,"success":true}</pre>
93 </body>
94 To retrieve the response data:
95 $('#ff').form({
96 success:function(data){
97 alert(data);
98 }
99 });
100 */
101 public ActionResult ToActionResult()
102 {
103 var result = new ContentResult();
104 result.Content = string.Format("<body><pre>{0}</pre></body>", this.ToString());
105 result.ContentType = "text/html";
106 return result;
107 }
108 }