Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 由于JSON太大,Ajax导致500个内部服务器错误_C#_Jquery_Json_Ajax_Asp.net Mvc 4 - Fatal编程技术网

C# 由于JSON太大,Ajax导致500个内部服务器错误

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调用

$.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。这个答案可能会有所帮助