1. 程式人生 > >解決JSON字符長度超出限制

解決JSON字符長度超出限制

pro script ssr req 摘要 void 文件中 back fig

環境asp.net 4.0 ,AJAX 一般處理程序

碰到JSON字符長度超出限制,拋出異常,參考了網上資料和網友博客

web.config添加以下內容

<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="20971520"/>
</webServices>
</scripting>
</system.web.extensions>

ashx業務文件中添加紅色代碼就OK

 1
using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Configuration; 6 using System.Web.Script.Serialization; 7 using MODEL; 8 using TOOL; 9 using BLL; 10 using System.Web.Configuration; 11 namespace UI.ajax.stock 12 { 13 /// <summary>
14 /// stockin 的摘要說明 15 /// </summary> 16 public class stockin : IHttpHandler 17 { 18 19 public void ProcessRequest(HttpContext context) 20 { 21 context.Response.ContentType = "text/plain"; 22 stockInBLL bll = new stockInBLL(); 23 List<MODEL.stockin> list = bll.getStockIn();
24 //序列化 25 JavaScriptSerializer jss = new JavaScriptSerializer(); 26 ScriptingJsonSerializationSection section = ConfigurationManager.GetSection("system.web.extensions/scripting/webServices/jsonSerialization") as ScriptingJsonSerializationSection; 27 28 if (section != null) 29 { 30 jss.MaxJsonLength = section.MaxJsonLength; 31 jss.RecursionLimit = section.RecursionLimit; 32 } 33 context.Response.Write(jss.Serialize(list)); 34 } 35 36 public bool IsReusable 37 { 38 get 39 { 40 return false; 41 } 42 } 43 } 44 }

解決JSON字符長度超出限制