2

Estoy enviando una lista grande de objectos, sin embargo al enviarlos por una petición ajax me sale el error: "Json demasiado grande":

function savePermissions(pLstObjPermissions) {
            $.ajax({
                url: "/Security/SavePermission",
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify(pLstObjPermissions),
                success: function (response) {

                    if (response.success) {
                        showAlert("Permisos web", "Has actualizado correctamente los permisos web.", "success");
                        resetForm();
                    }
                    else {
                        showMessage("Permisos web", response.message, "error");
                    };
                },
                error: function (request, status, exception) {
                    showMessage("Permisos web", exception, "error");
                }
            });
        };

Donde pLstObjPermissions es igual a:

[{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":1,"AccessId":3,"AllowAccess":true},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":27,"AllowAccess":true},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":22,"AllowAccess":true},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":23,"AllowAccess":true},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":24,"AllowAccess":true},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":25,"AllowAccess":true},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":26,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":1,"AccessId":4,"AllowAccess":true},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":14,"AllowAccess":true},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":15,"AllowAccess":true},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":1,"AccessId":5,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":40,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":39,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":47,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":48,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":41,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":42,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":43,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":44,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":45,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":38,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":18,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":19,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":17,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":46,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":20,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":1,"AccessId":6,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":13,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":1,"AccessId":7,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":9,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":10,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":11,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":12,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":1,"AccessId":12,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":30,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":31,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":32,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":1,"AccessId":8,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":33,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":34,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":35,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":36,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":1,"AccessId":9,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":7,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":8,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":1,"AccessId":10,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":1,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":2,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":3,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":4,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":21,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":37,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":1,"AccessId":11,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":5,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":2,"AccessId":6,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":1,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":2,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":3,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":4,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":5,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":6,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":7,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":8,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":9,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":10,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":11,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":12,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":13,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":14,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":15,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":16,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":17,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":18,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":19,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":20,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":21,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":22,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":23,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":24,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":25,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":26,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":27,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":28,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":29,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":30,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":31,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":32,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":33,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":34,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":35,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":36,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":37,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":38,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":39,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":40,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":41,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":42,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":43,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":44,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":45,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":46,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":47,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":48,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":49,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":50,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":51,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":52,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":53,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":54,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":55,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":56,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":57,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":58,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":59,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":60,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":61,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":62,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":63,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":64,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":65,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":66,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":67,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":68,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":69,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":70,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":71,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":72,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":73,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":74,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":75,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":76,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":77,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":78,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":79,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":80,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":81,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":82,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":83,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":84,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":85,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":86,"AllowAccess":false},{"Id":0,"PermissionType":2,"PermissionId":0,"AccessType":5,"AccessId":87,"AllowAccess":false}]

Son como 256 objetos en la lista

Del lado del servidor el codigo es el siguiente:

public ActionResult SavePermission(IList<Permission> pLstObjPermissions)
        {
            var lLstObjResult = new Dictionary<string, object>();

            try
            {
                SecurityFactory.GetPermissionService().SaveOrUpdatePermissions(pLstObjPermissions);
                lLstObjResult.Add(MESSAGE, "Registro guardado exitosamente");
                lLstObjResult.Add(SUCCESS, true);
            }
            catch (Exception e)
            {
                lLstObjResult.Add(MESSAGE, e.Message);
                lLstObjResult.Add(SUCCESS, false);
            }

            return Json(lLstObjResult, JsonRequestBehavior.AllowGet);
        }

**Nota: en el web.config ya tengo especificado el tamaño de las peticiones.

  <system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <sessionState mode="InProc" cookieless="false" timeout="120"/>
    <!--<customErrors mode="On" >-->
    <customErrors mode="RemoteOnly" >
      <error statusCode="401" redirect="/Error/401" />
      <error statusCode="402" redirect="/Error/402" />
      <error statusCode="403" redirect="/Error/403" />
      <error statusCode="404" redirect="/Error/404" />
      <error statusCode="405" redirect="/Error/405" />
      <error statusCode="406" redirect="/Error/406" />
      <error statusCode="407" redirect="/Error/407" />
      <error statusCode="408" redirect="/Error/408" />
      <error statusCode="409" redirect="/Error/409" />
      <error statusCode="410" redirect="/Error/410" />
      <error statusCode="411" redirect="/Error/411" />
    </customErrors>  
    <httpRuntime targetFramework="4.6.1" maxRequestLength="65536" />
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
  </system.web>

2 respuestas 2

3

Se debe agregar el elemento aspnet:MaxJsonDeserializerMembers a la sección <appSettings> de tu archivo web.config, poniendo como valor 2147483647 que es el equivalente al valor máximo de un tipo de dato Int32.

<configuration>
    <appSettings>
        <add key="aspnet:MaxJsonDeserializerMembers" value="2147483647" />
    </appSettings>
</configuration>

En está página de la documentación oficial, podrás encontrar toda la información acerca de los elementos así como su uso.

Nota: en la sección de la documentación oficial se recomienda no ajustar al valor máximo ya que representa un riesgo de seguridad. Lo ideal será que revises el número de elementos que deseas deserializar e intentar realizar un estimado tomando como referencia el tamaño máximo del json que estás usando.

1
  • 1
    Gracias por tu respuesta
    – anayarojo
    Commented el 13 oct. 2017 a las 2:39
1

Actualmente resolví este problema enviando la lista como un string y deserializandolo del lado del servidor, no creo que sea la mejor practica pero funciona de momento:

function savePermissions(pLstObjPermissions) {
            $.ajax({
                url: "/Security/SavePermission",
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify({pStrPermissions: JSON.stringify(pLstObjPermissions)}),
                success: function (response) {

                    if (response.success) {
                        showAlert("Permisos web", "Has actualizado correctamente los permisos web.", "success");
                        resetForm();
                    }
                    else {
                        showMessage("Permisos web", response.message, "error");
                    };
                },
                error: function (request, status, exception) {
                    showMessage("Permisos web", exception, "error");
                }
            });
        };
public ActionResult SavePermission(string pStrPermissions)
        {
            var lLstObjResult = new Dictionary<string, object>();

            try
            {
                SecurityFactory.GetPermissionService().SaveOrUpdatePermissions(JsonConvert.DeserializeObject<IList<Pemission>>(pStrPermissions));
                lLstObjResult.Add(MESSAGE, "Registro guardado exitosamente");
                lLstObjResult.Add(SUCCESS, true);
            }
            catch (Exception e)
            {
                lLstObjResult.Add(MESSAGE, e.Message);
                lLstObjResult.Add(SUCCESS, false);
            }

            return Json(lLstObjResult, JsonRequestBehavior.AllowGet);
        }

Si tienen una mejor manera, favor de compartirla.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.