Friday, 16 December 2016

Route 'controller/action' threw Exception System.InvalidOperationException: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

Solution

Set the MaxJsonLength property in web.config or controller as below.
<system.web.extensions>
  <scripting>
    <webServices>
      <jsonSerialization maxJsonLength="2147483644"/>
    </webServices>
  </scripting>
</system.web.extensions>
If it does not work then try to write like below.
public ActionResult ControllerAction()
{
  var jsonResult = Json(veryLargeCollection, JsonRequestBehavior.AllowGet);
  jsonResult.MaxJsonLength = int.MaxValue;
  return jsonResult;
}

If with above code still you are getting this error then use NewtonSoft.Json code.
public JsonResult ControllerAction()
        {
            var data= service.LoadApplicationUserEntitlements();
            var searializeddata = JsonConvert.SerializeObject(data);
            return Json(new { Success = true, Data = searializeddata }, JsonRequestBehavior.AllowGet);
        }

and in Script file use JSON.Parse method to get back object data as below.


In success function of call use

var data = JSON.Parse(data.Data);




No comments:

Post a Comment