在jQuery/Javascript中增加POST数据大小

Luk*_*kas 2 javascript c# jquery json web-services

可能重复:
我可以在web.config中为maxJsonLength设置无限长度吗?

我的.aspx页面中有一个脚本,它将信息发送到后端.cs页面.在大多数情况下,概念很简单,最差,除非数据太大.如何在不修改web.config的情况下增加"数据"变量的容量?请参阅下面的代码.

.ASPX

<script type="text/javascript">
    $(document).ready(function () {
        var note = "";
        for (var i = 0; i < 200000; i++)
            note = note + "x";

        $.ajax({
            type: "POST",
            url: "GroupDetailsDisplayPlus.aspx/UpdateRecord",
            data: "{note: \"" + note + "\"}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                var response = msg.d;
                alert("success");
                }
            error: function (request, status, thrownError) {
                //alert(request.thrownError); // short version
                alert(request.responseText);  // long version
            }
        });
    });
</script>
Run Code Online (Sandbox Code Playgroud)

.CS

    [System.Web.Services.WebMethod]
    public static string UpdateRecord(string note)
    {
        return note;
    }
Run Code Online (Sandbox Code Playgroud)

这段代码很简单,我的目的是将这个大字符串存储在数据库中(代码省略).如果我将for循环设置为仅执行100,000个循环,则此方法有效.但是将其增加到200,000个周期会失败,并显示错误消息:

{"Message":"使用JSON JavaScriptSerializer进行序列化或反序列化时出错.字符串的长度超过了maxJsonLength属性上设置的值.\ r \nParameter name:input","StackTrace":在System.Web.Script中. Serial.JavaScriptSerializer.Deserialize(JavaScriptSerializer序列化程序,字符串输入,类型类型,Int32 depthLimit)\ r \n在System.Web.Script.Serialization.JavaScript Serializer.Deserialize [T](字符串输入)\ r \n在System.Web上.Script.Service.RestHandler.ExecuteWebServiceCall(HttpContext context,WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

感谢您的帮助.

Bor*_*pov 11

试试这个:

<system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="500000">
        </jsonSerialization>
      </webServices>
    </scripting>
</system.web.extensions>
Run Code Online (Sandbox Code Playgroud)

还有这个:

<system.web>
  <httpRuntime requestValidationMode="2.0" executionTimeout="600" maxRequestLength="2000000" />
<system.web>
Run Code Online (Sandbox Code Playgroud)

或划分您的数据并逐个发送:

var portionIndex = 0;
var porions = new Array();
for(i = 0; i < 5; i++)
{
    var note = '';
    for (var j = 0; j < 40000; j++) note += "x";
    portions.push(note);
}

SendPortion();

function SendPortion()
{
        $.ajax({
            type: "POST",
            url: "GroupDetailsDisplayPlus.aspx/UpdateRecord",
            data: {porionsCount: porions.length, portion: porions[portionIndex] },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                portionIndex++; 
                if(portionIndex < porions.length) 
                    SendPortion();
            }
            error: function (request, status, thrownError) {}
        });
}
Run Code Online (Sandbox Code Playgroud)