简体   繁体   English

由于大型JSON,Ajax导致500 Internal Server Error

[英]Ajax results in 500 Internal Server Error because of large JSON

I have a the following code 我有以下代码

ajax call 阿贾克斯电话

$.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

}
});

and on the server-side 在服务器端

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

result variable can be small but sometimes its a very big object. 结果变量可以很小,但有时它是一个很大的对象。 when the object is small enough the code works and the ajax success code is activated but when the result is big enough the code fails with 500 Internal Server Error. 当对象足够小时,代码可以工作并且激活了ajax成功代码,但是当结果足够大时,代码将失败,并显示500 Internal Server Error。 I cant put MaxJsonLength any bigger because its an int but sometimes I need to send some big data.. what can I do? 我不能将MaxJsonLength设置得更大一些,因为它是int,但是有时我需要发送一些大数据。我该怎么办?

Use the code below in web.config . 使用以下web.config的代码。 It will solve your problem. 它将解决您的问题。

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

当我遇到大型json数据的类似问题时,Web配置中maxJsonLength的增加对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM