length of the string exceeds the value set on the maxJsonLength property

10,989

Thanks to Ed Gibbs and @NextInLine 's suggestion. I did the fix as below and it work like a charm now. I also removed the "system.web.extensions" portion away from my web.config

[WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public void GetData(string login)
        {

            // when the amount of data return is huge
            var serializer = new JavaScriptSerializer();

            // we need to do this.
            serializer.MaxJsonLength = Int32.MaxValue;


            var result = serializer.Serialize(service.GetData(login));


            Context.Response.Write(result);
        }
Share:
10,989
George Huang
Author by

George Huang

Updated on June 19, 2022

Comments

  • George Huang
    George Huang almost 2 years

    I have a .Net Web service (.asmx) which will return a Json string to my client. However, some of my data is really large and I get this error occasionally.

    The length of the string exceeds the value set on the maxJsonLength property.

    I've changed the maxJsonLength property to 2147483644, but it still doesn't work. Please help... Thank you.

     <system.web.extensions>
        <scripting>
          <webServices>
            <jsonSerialization maxJsonLength="2147483644"/>
          </webServices>
        </scripting>
      </system.web.extensions>
    
    
    
    [WebMethod]
            [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
            public void GetData(string login)
            {
                // throw an error on this line...
                string result = new JavaScriptSerializer().Serialize(service.GetData(login));
    
    
                Context.Response.Write(result);
            }