Ajax调用返回用户错误:字符串的长度超过了在maxJsonLength属性上设置的值

时间:2019-05-29 12:37:49

标签: javascript jquery asp.net-mvc

我正在使用对我的控制器的ajax调用,该调用将大约1万名用户的数组返回到我的视图,该视图用于固定用户。但是,我收到此错误。

我阅读了很多文章,看到了以下内容,并进行了尝试,但仍然遇到相同的问题:

试图将以下内容添加到我的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
                            });

我做错了什么吗?还是有另一种方式将所有用户传递到我的视图?

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

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字符串时,可以尝试使用此方法。

答案 1 :(得分:0)

因此,我能够找到解决问题的方法。感谢此链接:HANDLING LARGER JSON STRING VALUES SERIALIZATION IN MVC

下面是我使用的代码:

        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;
        }