2

I asked this question the other day but was not able to come up with a solution:

What happens between clicking a button and the Javascript method actually executing?

Posting a new question with better information including a screenshot of the ajax request, the details from the request / response, as well as my own understanding of what was going on yesterday more clearly so I could debug it better.

My jQuery sends the following request to my server:

enter image description here

The RtfErrorList in data is north of 4 million characters and includes RTF encoding.

Despite adding a variety of XML nodes to my web.config to increase the the maxJsonLength, I'm still getting this exception.

If I look at the network tab in Chrome's dev tools, I get the following information (I stripped some unimportant bits out like origin: locahost and other things like that)

Request Method:POST

Status Code:500 Internal Server Error

Response Headers:

HTTP/1.1 500 Internal Server Error

Content-Type: text/html; charset=utf-8

Server: Microsoft-IIS/7.5

X-AspNet-Version: 4.0.30319

X-UA-Compatible: IE=Edge

Request Headers:

Content-Length: 3799356

Content-Type: application/json;

Accept: text/html, /; q=0.01

X-Requested-With: XMLHttpRequest

Request Payload:

FileName : "someFileName"

RtfErrorList : "some4MillionCharacterStringIncludingRtfEncoding"

The last few entries from the stack trace:

[ArgumentException: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property. Parameter name: input]

System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)

System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext)

I've tried adding the following keys to my web.config:

<add key="JSONMAXJSONLENGTH" value="2147483644" />
<add key="aspnet:MaxJsonDeserializerMembers" value="2147483644" />

I'm actually not sure what that first node is from; it was here before me.

As well as the snippet found here.

Per this SO answer it is my understanding these web.config values are used only by the internal JavaScriptSerializer class, not any "custom" code I might write in a controller myself. In that case, this should be fine as it is System.web.SCript.Serialization.javaScriptSerializer.Deserialize that is throwing the exception. This should be using the web.config value, right?

I cannot figure out what could possibly be causing this...

Community
  • 1
  • 1
sab669
  • 3,984
  • 8
  • 38
  • 75
  • Possible duplicate of [Can I set an unlimited length for maxJsonLength in web.config?](http://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config) – Arasu RRK Oct 12 '16 at 19:51
  • @ArasuRRK I've tried everything in all the various threads citing this same error. And also I linked that thread in my question both today and what I had posted yesterday. – sab669 Oct 12 '16 at 19:56
  • you might want to add some code (or pseudo code) so that we can reproduce issue on our end and provide the solution. Image doesn't help much. – Imad Aug 27 '19 at 06:23
  • @sab669 did you get the solutions of this problem, i am stuck in same problem. Will be really help you if you reply. – Waqas Arif Apr 14 '20 at 12:59

1 Answers1

0

Are you expecting to receive html format as you set dataType:'html' in Ajax ?

Did you try this in your Ajax:

data: JSON.stringify(data),
contentType: 'application/json; charset=utf-8',
dataType: 'json',

Moreover, according to the documentation, the value of the MaxJsonLength property applies only to the internal JavaScriptSerializer instance that is used by the asynchronous communication layer to invoke Web services methods.

In this case, you may need to serialize manually in the controller. For instance:

public ActionResult FOO()
{
    var serializer = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue };


    // You could also read MaxJsonLength from your config 
    //serializer.MaxJsonLength = Int32.MaxValue;

    var myLargeData = new {
        ID = "5",
        Foo = "Bar",
        Value = "foo"
    };
    var result = new ContentResult
    {
        Content = serializer.Serialize(myLargeData),
        ContentType = "application/json"
    };
    return result;
}
A. Nadjar
  • 2,440
  • 2
  • 19
  • 20