Jquery ajax post使用大型JSON对象失败

Jquery ajax post使用大型JSON对象失败,jquery,ajax,json,post,Jquery,Ajax,Json,Post,我正在将JSON数据发布到ASP.NET MVC2服务器。我正在发布大型JSON字符串(包含一个从本地文件系统读取的base64编码文件流)。jQueryAjax调用在文件大小达到2.5Mb左右时工作良好。一旦超过这个大小,ajax调用就会失败(永远不会到达控制器)。我无法准确检测错误是什么-它似乎没有填充错误变量 ajax调用如下所示: $.ajax({ type: "POST", dataType: 'json', ti

我正在将JSON数据发布到ASP.NET MVC2服务器。我正在发布大型JSON字符串(包含一个从本地文件系统读取的base64编码文件流)。jQueryAjax调用在文件大小达到2.5Mb左右时工作良好。一旦超过这个大小,ajax调用就会失败(永远不会到达控制器)。我无法准确检测错误是什么-它似乎没有填充错误变量

ajax调用如下所示:

$.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));
            }
        });
$.ajax({
类型:“POST”,
数据类型:“json”,
超时:10000,
url:“/Molecule/SaveMolecule”,
数据:{jsonpost:postdata,molecletype:_molecletype,moleclefilestream:_moleclefilestream,changedproducts:stringifiedChangedProducts},//注意此处添加的molecletype
成功:功能(数据){
如果(data.rc==“成功”){
$.log('ServerSuccess:'+data.message);
分子_updateLocalInstance();
_b分子密度=假;
if(breturntomoleclelist!=null&&breturntomoleclelist==true){
导航器_Go('/molecular/Index/'+_moleculeType);
}
否则{
_saveMoleculeButtonFader=setTimeout(函数(){
$('#profilesave container').delay(500).html('+#术语+'已保存').fadeIn(500);
_saveMoleculeButtonFader=setTimeout(函数(){$('#profilesave container').fadeOut(1000);},2000);
}, 500);
}
}否则{
$.log('serverunful:'+data.message);
RemoveMoleculeExitDialog();
}
}
,错误:函数(jqXHR,textStatus,errorshown){
警报('Save failed,检查控制台是否有错误消息:'+textStatus+'+errorshown);
MarkMoleculeAsDirty();
$.log('molecular Save Error:'+helper_objectToString(textStatus+''+errorshown));
}
});
其中_moleculefilestream是大型base64编码流

My web.config包括以下内容:

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


有人有什么好主意吗?

尝试设置httpRuntime的maxRequestLength属性


您可以通过位置标签将其设置为所需的控制器/操作。

您是否尝试过调整超时?对于2.5Mb,10秒可能足够了,但不能超过。

更新

必须将a
spnet:maxjsonserializermembers
元素添加到web.config文件的
部分,设置为2147483647,这相当于
Int32
数据类型的最大值

<configuration>
  <appSettings>
    <add key="aspnet:MaxJsonDeserializerMembers" value="2147483647" />
  </appSettings>
</configuration>
然后,在服务器端,在web配置中更改请求长度:

<httpRuntime targetFramework="4.6.1" maxRequestLength="65536"/>
这些只是建议。唯一对我有效的方法是序列化列表并在服务器上反序列化它

例如,在客户端:

函数保存权限(pLstObjPermissions){
$.ajax({
url:“/Security/SavePermissions”,
类型:“POST”,
数据类型:“json”,
contentType:“应用程序/json;字符集=utf-8”,
数据:JSON.stringify({pStrPermissions:JSON.stringify(pLstObjPermissions)}),
成功:功能(响应){
if(response.success){
面板。显示警报(“Permisos”,“Se han Projectizado correctamente los Permisos.”,“成功”);
resetForm();
}
否则{
$.showMessage(“Permisos”,response.message,“error”);
};
},
错误:功能(请求、状态、异常){
$.showMessage(“Permisos”,异常,“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);
}
public ActionResult保存权限(字符串pStrPermissions)
{
var lLstObjResult=新字典();
尝试
{
SecurityFactory.GetPermissionService().UpdateList(JsonConvert.DeserializeObject(PSTRP));
添加(消息,“Registro guardado exitosamente”);
lLstObjResult.Add(SUCCESS,true);
}
捕获(例外e)
{
lLstObjResult.Add(MESSAGE,e.MESSAGE);
lLstObjResult.Add(成功,错误);
}
返回Json(lLstObjResult,JsonRequestBehavior.AllowGet);
}
我知道这不是最好的方法,但在更好的方法出现之前,它是有效的


如果您有更好的解决此问题的方法,请与我们分享。

谢谢您的提示Erick-我正在本地测试此方法,因此10s对于大文件来说已经足够了,但我接受您对实时环境的观点,并将相应地增加超时时间。
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);
}