[英]The length of the string exceeds the value set on the maxJsonLength property
[英]Ajax call returning users error: length of the string exceeds the value set on the maxJsonLength property
我正在使用对我的控制器的ajax调用,该调用将大约10k用户的数组返回到我的视图,该视图用于固定用户。 但是,我收到了这个错误。
我已经阅读了很多帖子并看到了以下内容,并尝试过,但仍然遇到同样的问题:
尝试将以下内容添加到我的web.config中:
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength = "2147483647"></jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>
我也把这个添加到我的actionresult:
var objJSS = new JavaScriptSerializer() { MaxJsonLength = Int32.MaxValue };
return Json(objJSS.Serialize(pinnedUsers), JsonRequestBehavior.AllowGet);
以下是我的整个actionresult
:
public ActionResult GetPinnedUsers()
{
clsComments clsComments = new clsComments();
PinnedUsersViewModel[] pinnedUsers;
pinnedUsers = clsComments.GetPinnedUsers();
//var jsonResult = Json(pinnedUsers, JsonRequestBehavior.AllowGet);
//jsonResult.MaxJsonLength = int.MaxValue;
var objJSS = new JavaScriptSerializer() { MaxJsonLength = Int32.MaxValue };
return Json(objJSS.Serialize(pinnedUsers), JsonRequestBehavior.AllowGet);
}
以下是我的Ajax调用:
$.ajax({
type: 'get',
traditional: true,
url: '@Url.Action("GetPinnedUsers", "ILearn")',
success: function (usersArray) {
success(usersArray)
},
error: error
});
我做错了什么或遗失了什么吗? 或者是否有其他方式让所有用户都传递到我的视图?
谢谢你的帮助。
JavaScriptSerializer serializer = new JavaScriptSerializer(); serializer.MaxJsonLength = Int32.MaxValue; //Or any size you want to use, basically int maxValue is 2GB, you shouldn't need this big json string to deserialize, else you are doing it wrong. myObject obj = serializer.Deserialize<myObject>(yourJsonString);
您可以尝试反序列化JSON字符串时尝试此操作。
所以我能够找到解决问题的方法。 感谢这个链接: 在MVC中处理更大的JSON字符串值序列化
以下是我使用的代码:
public ActionResult GetPinnedUsers()
{
clsComments clsComments = new clsComments();
PinnedUsersViewModel[] pinnedUsers;
//JavaScriptSerializer serializer = new JavaScriptSerializer();
//serializer.MaxJsonLength = Int32.MaxValue; //Or any size you want to use, basically int maxValue is 2GB, you shouldn't need this big json string to deserialize, else you are doing it wrong.
pinnedUsers = clsComments.GetPinnedUsers();
return SerializeJSON(pinnedUsers);
}
private ContentResult SerializeJSON(PinnedUsersViewModel[] users)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
var resultData = users;
ContentResult result = new ContentResult();
result.Content = serializer.Serialize(resultData);
result.ContentType = "application/json";
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.