字符串超过maxJsonLength且小于250kb [英] String exceeds maxJsonLength with less than 250kb

查看:77
本文介绍了字符串超过maxJsonLength且小于250kb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个实体与ajax一起使用。我希望在使用JavaScript创建的网格中使用实体框架提供完整的表格。我目前发送的表格少于140行。我的代码有效,如果我在表格中只有50行,我会收到以下错误:

I'm using an entity together with ajax. I want the full table i provice with Entity framework ina grid created with JavaScript. The table I'm currently sending has less than 140 lines. My code works if I only have 50 lines in the table a few more an I get the following error:

{"Message":"Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.","StackTrace":"   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj)\r\n   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

在我的网络统计数据中,我在我的响应正文中得到了500个错误的XMLHttpRequest如上所示的错误。如果我将行数限制为50,我可以让页面正常工作,我可以在响应正文中看到我的数据。以下代码是将实体数据发送到javascript所需的所有代码。
JavaScript:

In my network stats I get a 500 error for my XMLHttpRequest in the response body I have the error as shown above. If I limit the amount of lines to 50 I can have the page working properly and I can see my data in the response body. The following code is all the code you need to send data from your entity to your javascript. JavaScript:

$(function() {
    $.ajax({
        url: "EditFeedApac.aspx/GetApacData",
        type: "POST",
        contentType: "application/json",
        success: function (result) {
            console.log(result.d);
        },
        error: showError
    });
});

C#

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static IEnumerable<PreApacData> GetApacData()
{
    var a = new Towers_WatsonEntities().tbl_TowersWatson_preAPAC.ToList();
    Mapper.CreateMap<tbl_TowersWatson_preAPAC, PreApacData>(); 
    var newData = a.Select(Mapper.Map<tbl_TowersWatson_preAPAC, PreApacData>).ToList(); 
    return newData;
}

正如您所看到的,错误清楚地表明MaxJsonLength尚未设置为最大值。
在GetApacData()函数中,我添加了以下代码:

As you can see the error clearly indicates MaxJsonLength hasn't been set to the max value. In the GetApacData() function I added the following code:

var serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
string test = serializer.Serialize(newData);
return test;

现在MaxJsonLength可能变成4MB的大小,只是为了取消限制。
使用此属性集我仍然会得到相同的序列化错误?
我在这里做错了什么?
如何将数据发送到我的JavaScript?

Now the MaxJsonLength could become the size of 4MB, just to take the limit off. With this property set I still get the same serialization error? What am I doing wrong here? How can I send my data to my JavaScript?

推荐答案

尝试通过

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

显然是你的

serializer.MaxJsonLength = Int32.MaxValue;

被覆盖。

这篇关于字符串超过maxJsonLength且小于250kb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆