0

my project is an MVC Web application I am using ajax call to get a huge list of data as JSON.

Here is the code( i am not sure what I a missing ):

  $.ajax({
            url: url, //server
            type: "POST",
            async: true,
            data: { id: id },
            dataType: "json",
            success: function (data) {
                debugger;
                jQuery.each(data, function (i, val) {

//Success code


            },
            error: function (xhr, ajaxOptions, thrownError) {
                console.log(xhr.status);
                console.log(thrownError);
                window.baseShowModalOkCancel("<p>Backlog Data</p>", "<p>Error in Database</p>", "ERROR");
            }
        });
    }

Action Controller:( This is a post method in return statement I can see the list of object with the data.

   [HttpPost]
    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public ActionResult GetBacklogWithData(string id)
    {
        using (var db = new LiensTrackerEntities())
        {

            List<BackLogDataList> backLogData = new List<BackLogDataList>();
            List<BackLogData> backLog;

               backLog = db.BackLogDatas.OrderBy(x =>x.CaseNumber).ToList();
                foreach (var item in backLog)
                {
                    BackLogDataList backLogDataList = new BackLogDataList
                    {
                        FileNo = item.FileNo,
                        BackLogId = item.BackLogID,
                        FirstName = item.FirstName,
                        LastName = item.LastName,
                        Middle = item.Middle,
                        Suffix = item.Suffix,
                        Address = item.Address,
                        Address2 = item.Address2,
                        City = item.City,
                        St = item.ST,
                        Zip = item.SP1ZIP,                           
                        AmaesRecordCreatedDate = item.AMAESRecordCreatedDate != null ? item.AMAESRecordCreatedDate.ToString().Split(' ')[0] : null,
                        AddedInRecipient = item.AddedInRecipient
                    };
                    backLogData.Add(backLogDataList);
                }

            }



            return Json(backLogData, JsonRequestBehavior.AllowGet);
        }
    }

It returns with 200 ok but execute the error function when i look into console i found the following error:

enter image description here

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Kotha
  • 87
  • 1
  • 1
  • 9
  • Please post the error and not an image. – Hackerman Dec 01 '17 at 17:33
  • is the response coming back a valid json array ? – Shyju Dec 01 '17 at 17:45
  • @Shyju , the response body is coming as HTML page. which is directly hitting to error function. – Kotha Dec 01 '17 at 19:18
  • @Hackerman. the ajax call response is directly hitting the error function. with 200 Result and error msg as: {description: "Invalid cha...", message: "Invalid cha...", name: "SyntaxError", number: -2146827274, stack: "SyntaxError..."} – Kotha Dec 01 '17 at 19:19
  • That means your server code is crashing and the server app is returning the html for the error view.your code is going to the error handler because you mentioned the expected data type is json. and the $.ajax is trying to parse that respones (assuming it is json) and crashing (because it is actually HTML. So you need to fix your code so that it will not return HTML – Shyju Dec 01 '17 at 19:30
  • Take a look at [this post](https://stackoverflow.com/questions/46531882/show-exception-message-to-client-side/46532062#46532062) which has solution to return a json response even in the case of an exception. – Shyju Dec 01 '17 at 19:35
  • @Shyju thanks for the suggestion. But my code looks straightforward, can you suggest where to do, Appreciate your help... – Kotha Dec 01 '17 at 19:35
  • See the comment i posted above your prev comment. – Shyju Dec 01 '17 at 19:36
  • @shyju Thanks, I found the error using your post. and fixed it. – Kotha Dec 01 '17 at 19:53

1 Answers1

0

Thee error is due to max json string length. the solution is

MaxJsonLength exception in ASP.NET MVC during JavaScriptSerializer

Kotha
  • 87
  • 1
  • 1
  • 9