title: The length of the string exceeds the value set on the maxJsonLength property (WebMethod)
body: <h3>Background</h3>
  
  <p>I have a <code>WebMethod</code> in the code-behind for an ASPX page:</p>
  
  <pre><code>[WebMethod]
  public static string GetServiceDetail()
  {
      //...
  }
  </code></pre>
  
  <p>I'm using jQuery to send an AJAX request to it:</p>
  
  <pre><code>$.ajax({
      type: "POST",
      url: url + "GetServiceDetail",
      data: JSON.stringify({}),
      contentType: "application/json; charset=utf-8",
      dataType: "json",
  })
  </code></pre>
  
  <p>This generally works correctly.</p>
  
  <h3>Problem</h3>
  
  <p>When the <code>WebMethod</code> returns about 88KB of data, the framework returns a <code>500 Internal Server Error</code> to the HTTP request.  The HTTP response body contains a bunch of .NET exception information in JSON format. When deserialised, the response has this information:</p>
  
  <blockquote>
    <p><code>System.InvalidOperationException</code>:</p>
    
    <p>Error during serialization or deserialization using the JSON <a href="https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" rel="nofollow"><code>JavaScriptSerializer</code></a>. The length of the string exceeds the value set on the <code>maxJsonLength</code> property.</p>
  </blockquote>
  
  <pre><code>   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)
     at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)
     at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)
     at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)
  </code></pre>
  
  <h3>Troubleshooting</h3>
  
  <ul>
  <li>I'm not using <a href="https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" rel="nofollow"><code>JavaScriptSerializer</code></a> in the <code>WebMethod</code> at all. As the stack trace shows, the exception is happening outside of my code.</li>
  <li>Existing StackOverflow questions seem to be about explicit JSON serialisation or different technologies. They imply changing the Web.config value doesn't work.</li>
  </ul>