JQuery.ajax error with large JSON object

I am sending JSON data to an ASP.NET MVC2 server. I am sending large JSON strings (which include a base64 encoded file stream read from the local file system). The jQuery ajax call works up to file sizes around 2.5Mb. Once above this size, the ajax call fails (never reaches the controller). I cannot determine exactly what the error is: it does not populate the error variables.

The ajax call is as follows:

$.ajax({
            type: "POST",
            dataType: 'json',
            timeout: 10000,
            url: "/Molecule/SaveMolecule",
            data: { jsonpost: postdata, moleculetype: _moleculeType, moleculefilestream: _moleculefilestream, changedproducts: stringifiedChangedProducts }, // NOTE the moleculeType being added here
            success: function (data) {
                if (data.rc == "success") {
                    $.log('ServerSuccess:' + data.message);

                    molecule_updateLocalInstance();

                    _bMoleculeIsDirty = false;
                    if (bReturnToMoleculeList != null && bReturnToMoleculeList == true) {
                        navigator_Go('/Molecule/Index/' + _moleculeType);
                    }
                    else {
                        _saveMoleculeButtonFader = setTimeout(function () {

                            $('#profilesave-container').delay(500).html('<img src="/content/images/tick.png" width="32px" height="32px" /><label>' + _moleculeSingularTerm + ' was saved</label>').fadeIn(500);

                            _saveMoleculeButtonFader = setTimeout(function () { $('#profilesave-container').fadeOut(1000); }, 2000);

                        }, 500);
                    }

                } else {
                    $.log('ServerUnhappy:' + data.message);
                    RemoveMoleculeExitDialog();
                }
            }
            , error: function (jqXHR, textStatus, errorThrown) {
                alert('Save failed, check console for error message:' +textStatus+' '+ errorThrown);
                MarkMoleculeAsDirty();
                $.log('Molecule Save Error:' + helper_objectToString(textStatus+' '+errorThrown));
            }
        });

where _moleculefilestream is a large base64 encoded stream.

My web.config includes the following:

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

Anyone have any bright ideas?

+5
source share
3 answers
+1

? 10 2.5Mb, .

+1

UPDATE

a spnet:MaxJsonDeserializerMembers <appSettings> web.config, 2147483647, Int32.

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

, .

. , . , , json, .

@Flxtr:

OLD

, FormData

:

function getDataForm() {

    var data = new FormData();

    var files = fileUploader.get(0).files;
    if (files.length > 0) {
        data.append("File", files[0]);
    }
    data.append("ImagePath", "");

    data.append("Id", ImageId);
    data.append("Name", txtName.val().trim());
    data.append("Description", txtDescription.val().trim());

    return data;
}

function save(data) {
    $.ajax({
        type: "POST",
        url: "/Files/SaveImage",
        contentType: false,
        processData: false,
        data: data,
        success: function (response) {

            if (response.success) {
                $.showMessage(messages.NAME, messages.SUCCESS, "success");
                closeForm();
                Files.ImageList.gridImages.ajax.reload();
            }
            else {
                $.showMessage(messages.NAME, response.message, "error");
            };

            btnSave.button('reset');
        },
        error: function (request, status, exception) {
            $.showMessage(messages.NAME, exception, "error");
            btnSave.button('reset');
        }
    });
};

-:

<httpRuntime targetFramework="4.6.1" maxRequestLength="65536"/>

:

<system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1" maxRequestLength="65536"/>
    <customErrors mode="RemoteOnly">
        <error statusCode="401" redirect="/Error/401" />
        ...
        <error statusCode="411" redirect="/Error/411" />
    </customErrors>
  </system.web>

processData false ajax:

$.ajax({
    url: "/Security/SavePermissions",
    type: "POST",
    processData: false,
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(pStrPermissions),
    success: function (response) {
        if (response.success) {
            panel.showAlert("Permisos", "Se han actualizado correctamente los permisos.", "success");
            resetForm();
        }
        else {
            $.showMessage("Permisos", response.message, "error");
        };
    },
    error: function (request, status, exception) {
        $.showMessage("Permisos", exception, "error");
    }
});

. , , - .

, :

function savePermissions(pLstObjPermissions) {
    $.ajax({
        url: "/Security/SavePermissions",
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ pStrPermissions: JSON.stringify(pLstObjPermissions)}) ,
        success: function (response) {
            if (response.success) {
                panel.showAlert("Permisos", "Se han actualizado correctamente los permisos.", "success");
                resetForm();
            }
            else {
                $.showMessage("Permisos", response.message, "error");
            };
        },
        error: function (request, status, exception) {
            $.showMessage("Permisos", exception, "error");
        }
    });
};

:

public ActionResult SavePermissions(string pStrPermissions)
{
    var lLstObjResult = new Dictionary<string, object>();

    try
    {
        SecurityFactory.GetPermissionService().UpdateList(JsonConvert.DeserializeObject<IList<Permission>>(pStrPemissions));
        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);
}

I know this is not the best way, but it works until something better appears.

If you have a better way to solve this problem, share it.

+1
source

All Articles