2

Possible Duplicate:
Can I set an unlimited length for maxJsonLength in web.config?

I have a script in my .aspx page that is sending info to the back-end .cs page. Concept is simple and worst for most occasions, except when data is too large. How can I increase the capacity of what the "data" variable can hold without modifying web.config? Please see code below.

.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;
    }

This code is simplified and my purpose is to store this large string in a database (code ommited). If I set the for loop to only do 100,000 cycles this works. However increasing it to 200,000 cycles fails with an error message:

{"Message":"Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.\r\nParameter name: input","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"}

Thank you for any and all help.

Community
  • 1
  • 1
Lukas
  • 2,885
  • 2
  • 29
  • 31
  • 1
    http://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config – Madbreaks Nov 21 '12 at 18:07
  • The question marked as duplicate talk about ASP.NET MVC and web.config. This question is talking about jQuery/JavaScript. If I am not mistaken, then, how is this a duplicate? – Phil Sep 23 '14 at 17:55
  • I even put it in bold, "WITHOUT modifying web.config." The above answer is distinctively asking for modifying web.config... Do people get points for reporting duplicates or are these individuals just having trouble reading? – Lukas May 04 '15 at 22:07

1 Answers1

11

Try this:

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

and this:

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

or divide your data and send part by part:

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) {}
        });
}
Boris Gappov
  • 2,483
  • 18
  • 23
  • The web.config workaround works, however I wasn't specific enough. I need something that I can do in front side (.aspx page) and not web.config. That's my bad and I will give you a point for this and I modified my original question. – Lukas Nov 21 '12 at 18:15