解决JSON字符长度超出限制
Posted 每天进步一点
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决JSON字符长度超出限制相关的知识,希望对你有一定的参考价值。
环境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字符长度超出限制的主要内容,如果未能解决你的问题,请参考以下文章