C# 由于JSON太大,Ajax导致500个内部服务器错误
我在下面的代码中有一个 ajax调用C# 由于JSON太大,Ajax导致500个内部服务器错误,c#,jquery,json,ajax,asp.net-mvc-4,C#,Jquery,Json,Ajax,Asp.net Mvc 4,我在下面的代码中有一个 ajax调用 $.ajax({ type: "POST", dataType: "json", async: false, contentType: "application/json; charset=utf-8", url: "Account/SomeFunc", data: JSON.stringify(dataToSend), success: function (res) { //some code }, error: function (res, tex
$.ajax({
type: "POST",
dataType: "json",
async: false,
contentType: "application/json; charset=utf-8",
url: "Account/SomeFunc",
data: JSON.stringify(dataToSend),
success: function (res) {
//some code
},
error: function (res, textStatus, errorThrown) {
//some code
}
});
在服务器端
public ActionResult SomeFunc()
{
//some code
VeryBigObject result = getResult();
if (result == null)
return null;
var jsonResult = Json(result, JsonRequestBehavior.AllowGet);
jsonResult.MaxJsonLength = int.MaxValue;
return jsonResult;
}
结果变量可以很小,但有时它是一个非常大的对象。当对象足够小时,代码工作,ajax成功代码被激活,但当结果足够大时,代码失败,出现500个内部服务器错误。
我不能把MaxJsonLength放得更大,因为它是一个int,但有时我需要发送一些大数据。。我能做些什么?当我在处理大量json数据时,在Web配置中增加maxJsonLength对我来说很有效。使用
Web.config
中的以下代码。它会解决你的问题
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="900000" />
</webServices>
</scripting>
</system.web.extensions>
您需要检查服务器配置的最大post大小并增加它。增加web.config
文件中的maxAllowedContentLength
。但是,默认限制是4Mb IIRC,您真的不应该在一个请求中发送那么多JSON。这个答案可能会有所帮助