参考
开发环境
名称 | 版本 |
---|---|
操作系统 | Windows 10 X64 |
VS | Visual Studio 2010 |
MVC | MVC3 |
问题描述
在一个 Controller 中返回 Json 数据时,页面报错
Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超过了为 maxJsonLength 属性设置的值。
原因分析
是因为 Json()
方法默认的返回值大小是 默认值为102400(100k)
问题解决
接口-设置 MaxJsonLength
//获取 Excel 数据
IList<IList<object>> listData = GetExcelData(worksheet);
//构造返回到前端的数据
var result = new
{
ColNames = listData[0],
ColModel = listData[1],
rows = listData[2],
fileName = fileName
};
//modify by 20191209 - return Json 默认值为102400(100k),获取截止当前期间会报错-Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.这里使用 string
var serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = int.MaxValue;
string json = serializer.Serialize(result);
return json;
//return Json(result, JsonRequestBehavior.AllowGet);
前端-字符转 Json-JSON.parse
由于返回的是字符串,因此需要把字符转为 Json
function (data) {
data = JSON.parse(data);
$("#fileName").val(data.fileName);
$("#tbTaxCollectionList").GridUnload();
var opts = global_Options.jqGridOptions();
}