Error during serialization or deserialization using the JSON JavaScriptSerializer

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

You should expecting the above error message in case you are trying to do an ajax call for a method that is returning large amount of data in Json format.

Solving this issue require to write some code to enlarge the size of maxJsonLength property’s value to inform the json serializer that this is a large size object. to do so, you will need to do something similar to below code snippet

public JsonResult GetLargeJson()
 {
     varresponse = GetLargeDataFromDataSource();
      //To handler large data results.
      jsonResultObj = new JsonResult();
      jsonResultObj.Data = response;
      jsonResultObj.MaxJsonLength = Int32.MaxValue;
      jsonResultObj.ContentType = "application/json";
 
      return jsonResultObj;
  }