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

时间:2012-11-21 18:03:48

标签: c# javascript jquery json web-services

  

可能重复:
  Can I set an unlimited length for maxJsonLength in web.config?

我的.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>

.CS

    [System.Web.Services.WebMethod]
    public static string UpdateRecord(string note)
    {
        return note;
    }

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

  

{“Message”:“使用序列化或反序列化时出错   JSON JavaScriptSerializer。字符串的长度超过该值   在maxJsonLength属性上设置。\ r \ nParameter name:   输入“,”StackTrace“:at   System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer   serializer,String input,Type type,Int32 depthLimit)\ r \ n at   System.Web.Script.Serialization.JavaScript   Serializer.Deserialize [T](String input)\ r \ n at   System.Web.Script.Service.RestHandler.ExecuteWebServiceCall(HttpContext的   context,WebServiceMethodData   methodData)”, “ExceptionType”: “System.ArgumentException”}

感谢您的帮助。

1 个答案:

答案 0 :(得分:11)

试试这个:

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

和此:

<system.web>
  <httpRuntime requestValidationMode="2.0" executionTimeout="600" maxRequestLength="2000000" />
<system.web>

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

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) {}
        });
}