EF阿贾克斯:字符串超过maxJsonLength小于250KB [英] EF to Ajax: string exceeds maxJsonLength with less than 250kb
问题描述
我使用的是实体与阿贾克斯。我想全表我provice与使用JavaScript创建的实体框架INA网格。我目前正在发送的表有不到140线。我的code的作品,如果我只在表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错误我XMLHtt prequest响应中的正文我有错误,如上图所示。如果我限制线的量到50,我可以在页面正常工作,我可以看到在响应正文我的数据。下面code是所有你需要从你的实体数据发送到您的JavaScript中的code。 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()函数中添加以下code:
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>
Apperently您
Apperently your
serializer.MaxJsonLength = Int32.MaxValue;
被覆盖。
这篇关于EF阿贾克斯:字符串超过maxJsonLength小于250KB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!