Nab*_*eel 11 ajax asp.net-mvc jquery json
我在razor视图引擎MVC4(.net 4.5)应用程序中加载大型JSON响应表单服务器时出现以下错误
" 使用JSON JavaScriptSerializer进行序列化或反序列化时出错.字符串的长度超过了@ Html.Raw(Json.Encode(jsondata)上的maxJsonLength属性上设置的值 "
我试过在我的web.config中设置MaxJsonLength属性:
configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
Run Code Online (Sandbox Code Playgroud)
在发送JSON响应的同时在服务器端尝试跟随.
return new JsonResult()
{
Data = data,
ContentType = contentType,
ContentEncoding = contentEncoding,
JsonRequestBehavior = behavior,
MaxJsonLength = Int32.MaxValue
};
Run Code Online (Sandbox Code Playgroud)
还尝试了列出的解决方案:http://brianreiter.org/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/.但没有什么对我有用:(
有人可以建议我如何避免这个错误或如何增加杰森响应最大长度?
Nab*_*eel 22
不知何故,我通过在视图中使用以下代码摆脱了这个错误.
@{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
}
<script type="text/javascript">
var entries = @Html.Raw(serializer.Serialize(Model.FeedEntries));
</script>
Run Code Online (Sandbox Code Playgroud)
至少对我来说,这不是在服务器端工作.
Emp*_*052 19
我的便士解决方案.b)因为a)在Mvc 4.5 AFAIK中给出了错误消息'System.Web.Mvc.JsonResult不包含maxJsonLength ...的定义',这是唯一有效的解决方法.
我把b)放在我的控制器里.希望这会对某人有所帮助.
此致,SM
一个)
var jsonResult = Json(list, JsonRequestBehavior.AllowGet);
jsonResult.maxJsonLength = int.MaxValue;
return jsonResult;
Run Code Online (Sandbox Code Playgroud)
b)
if (Request.IsAjaxRequest())
{
//Working solution
var serializer = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue, RecursionLimit = 100 };
return new ContentResult()
{
Content = serializer.Serialize(list),
ContentType = "application/json",
};
//Trial 2
//var jsonResult = Json(list, JsonRequestBehavior.AllowGet);
//jsonResult.maxJsonLength = int.MaxValue;
//return jsonResult;
//Trial 1
//return Json(list, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)