使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超过了为 maxJsonLength 属性设置的值。

 

一、Mvc中

//post请求
[HttpPost]
public JsonResult Info (string name)
{
            var data ="测试数据,你可以搞多点";    
            return new JsonResult() { Data = data, MaxJsonLength = int.MaxValue, ContentType = "application/json" };
}


//get请求 
[HttpGet]
public JsonResult Info (string name)
{
            var data ="测试数据,你可以搞多点";    
            return new JsonResult() { Data = data, MaxJsonLength = int.MaxValue, ContentType = "application/json" , JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
 加上: JsonRequestBehavior = JsonRequestBehavior.AllowGet   

 

二、借鉴来源:

https://www.cnblogs.com/ZJ199012/p/9946900.html

不同之处:一个是ActionResult ,一个是JsonResult。

 

三、失败经历:

此方案对JsonResult无效;

https://www.cnblogs.com/wangfuyou/p/6046307.html

1.解决办法是在web.config增加如下节点到<configuration>下

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

2.

JavaScriptSerializer jsSerializer  = new JavaScriptSerializer();
jsSerializer.MaxJsonLength = Int32.MaxValue;

 

posted @ 2019-06-24 19:03  hao_1234_1234  阅读(1505)  评论(0编辑  收藏  举报