locked
Error during serialization or deserialization using the JSON JavaScriptSerializer RRS feed

  • Question

  • User-1634604574 posted
    controller
    
     public JsonResult read_file_css(string js, string css)
            {
                SqlCommand com = new SqlCommand("read_file_css", con);
                com.CommandType = CommandType.StoredProcedure;
              //  com.Parameters.AddWithValue("@js", js);
                com.Parameters.AddWithValue("@css", css);
    
                SqlDataAdapter da = new SqlDataAdapter(com);
                DataSet ds = new DataSet();
                da.Fill(ds);
                List<currency_Detail> listreg = new List<currency_Detail>();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
    
                    listreg.Add(new currency_Detail
                    {
    
                       // JS = dr["JS"].ToString(),
                         CSS = dr["CSS"].ToString(),
    
    
                    });
                }
                return Json(listreg, JsonRequestBehavior.AllowGet);
    
            }
    
    
     view
    
       $.ajax({
                    type: 'GET',
                    url: '/Print_Format/read_file_css',
                    data: {
                        css: $("#name").val(),
                    },
                    success: function (response) {
                        data = $.map(response, function (item, a) {
                            
                            $("#custom_css").val(item.CSS)
    
    
                        });
                    }
                })

    i get this error

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

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

    Wednesday, October 2, 2019 8:58 AM

Answers

  • User665608656 posted

    Hi zhyanadil,

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

    This error shows that the json parameter you pass exceeds the maximum allowable length.

    To solve this issue, you can do the following within your Controller (or anywhere really):

    var serializer = new JavaScriptSerializer();
    
    // For simplicity just use Int32's max value.
    // You could always read the value from the config section mentioned above.
    serializer.MaxJsonLength = Int32.MaxValue;
    
    var resultData = new { Value = "foo", Text = "var" };
    var result = new ContentResult{
        Content = serializer.Serialize(resultData),
        ContentType = "application/json"
    };
    return result;

    For more details, you could refer to these links: Can I set an unlimited length for maxJsonLength in web.config?

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 2, 2019 9:40 AM