【问题标题】:Ajax results in 500 Internal Server Error because of large JSON由于 JSON 较大,Ajax 导致 500 Internal Server Error
【发布时间】:2017-02-18 03:45:59
【问题描述】:

我有以下代码

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, 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 但有时我需要发送一些大数据。我能做什么?

【问题讨论】:

  • 你需要检查你的服务器配置max_post_size并增加它。
  • web.config 文件中增加maxAllowedContentLength。但是,默认限制是 4Mb IIRC,你真的,真的不应该在单个请求中发送那么多 JSON
  • 这个答案可能会有所帮助stackoverflow.com/a/1151993/4611027

标签: c# jquery json ajax asp.net-mvc-4


【解决方案1】:

web.config 中使用下面的代码。它会解决你的问题。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      相关资源
      最近更新 更多