0

I have a MVC controller where I am converting the datatable result using JsonConvert.SerializeObject and returning as follows

public JsonResult GetJson(DateTime StartDate)
        {
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(context.Database.Connection.ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("usp_GetDetails"))
                {
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@startdate", SqlDbType.DateTime);
                    cmd.Parameters["@startdate"].Value = StartDate;
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        sda.Fill(dt);
                    }
                }
            }
            return Json(new { success = true, data = JsonConvert.SerializeObject(dt) },
                JsonRequestBehavior.AllowGet);
        }

But the same is working inside fiddle when I copy the result that I am getting

https://jsfiddle.net/szq2ker4/1/ This is how I am making an ajax call in jquery and doing the rest as per fiddle but I am not seeing the expected as per in the fiddle

$.get("/controller/GetJson", { StartDate: date},
function (response) { var result = JSON.parse(response.data);})

The result I am getting is as follows, can some one let me know where I am doing wrong

enter image description here

Console response enter image description here

$.get("/controller/GetJson", { StartDate: date},
function (response) { var result = JSON.parse(response.data);}).fail(function (response) { **alert('failed')**;});
Learner
  • 351
  • 7
  • 25
  • what you get when you log `response` in `success`? – Guruprasad J Rao Mar 05 '18 at 11:24
  • Added the response – Learner Mar 05 '18 at 11:28
  • 1
    `return Json()` already serializes your object. Doing it a 2nd time using `JsonConvert.SerializeObject(dt)` and then using `JSON.parse(response.data)` makes no sense –  Mar 05 '18 at 11:28
  • You might only need to use `JSON.parse(response)` and yea as @StephenMuecke said there is no need to wrap it again with `JsonConvert.SerializeObject` – Guruprasad J Rao Mar 05 '18 at 11:29
  • If I use `return Json(new { success = true, data = dt }, JsonRequestBehavior.AllowGet);` it is giving me 500 error – Learner Mar 05 '18 at 11:34
  • What are the details of the error (that means that the server code is throwing an exception) Use you browser tools (the Network Tab) to inspect the response. (and I assume you have also changed it to just `var result = response.data;`) –  Mar 05 '18 at 11:36
  • After returning data as follows `Json(new { success = true, data = dt }` it is not entering in to this code at all`function (response) {` – Learner Mar 05 '18 at 11:38
  • It is givng me 500 error after controller returns – Learner Mar 05 '18 at 11:39
  • Read my previous comment - use your browser tools! –  Mar 05 '18 at 11:40
  • I have used and I am getting `500 internal server error` – Learner Mar 05 '18 at 11:41
  • I have added `fail` and it is entering in to that and when I see the response text it is showing as `500 internal server error` – Learner Mar 05 '18 at 11:44
  • FGS Use your browser tools to inspect the response - it contains the full details of your error!! –  Mar 05 '18 at 11:45
  • This is the error I am getting `A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'.` – Learner Mar 05 '18 at 12:19

1 Answers1

0

I found an answer to a question here on slack that may help you:

It seems that there are circular references in your object hierarchy which is not supported by the JSON serializer. Do you need all the columns? You could pick up only the properties you need in the view:

return Json(new 
{  
    PropertyINeed1 = data.PropertyINeed1,
    PropertyINeed2 = data.PropertyINeed2
});

This will make your JSON object lighter and easier to understand. If you have many properties, AutoMapper could be used to automatically map between DTO objects and View objects.

lat94
  • 511
  • 1
  • 5
  • 18