0

In my ASP.NET MVC app, with this AJAX call:

$.ajax({
    type: 'GET',
    url: '@Url.Action("GetUnitReportPairVals", "Home")',
    data: { unit: unitval, report: rptval }, // data: model,
    contentType: 'application/json',
    cache: false,
    success: function (result) {
        alert(result);
    },
    error: function (result) {
        alert('failed');
        alert(result);
    }
});

...calling this Controller method:

public JsonResult GetUnitReportPairVals(string unit, string report)
{
    int rptId = GetReportIDForName(report);

    DataTable UnitReportPairEmailValsDT = new DataTable();
    string qry = string.Format(SQL.UnitReportPairEmailQuery, unit, rptId);
    UnitReportPairEmailValsDT = SQL.ExecuteSQLReturnDataTable(
        qry, 
        CommandType.Text,
        null
        );

    var model = UnitReportPairEmailValsDT;
    return Json(model);
}

I see the "failed" alert, and the "result" object has no end of data; here's just the tip of the iceberg:

enter image description here

It seems the key thing is a Status of 500, and Status text of "Internal Server Error"

So I commented out those two lines and added in "debugger" in their stead (as suggested by nurdyguy here):

$.ajax({
    . . .
    error: function (result) {
        debugger;
        //alert('failed');
        //alert(result);
    }
});

...yet I don't see what that gets me. Putting the ajax call through its paces just shows nothing in the browser; using Chrome DevTools, I can step into it, but once I get to the "debugger" line, it doesn't do me any favors. Even F11 from that line does nothing.

So how can I get to the bottom of what's causing this 500/Internal Server Error?

UPDATE

Since I have commented out the "JSON" lines from the jQuery AJAX call var model = JSON.stringify({}) and contentType: 'application/json', should I also change the Controller return type from JsonResult to ActionResult and return View(model)?

UPDATE 2

I am getting a success from my AJAX method now with this, incorporating several suggestions from y'all and changing the model from a DataTable member to a generic list of string:

// Model (C#)
public class UnitReportPairModel
{
    public List<String> UnitReportPairEmailVals { get; set; }
    . . .
}   

// Controller (C#)
public JsonResult GetUnitReportPairEmailAddresses(string unit, string report)
{
    UnitReportPairModel model = new UnitReportPairModel();
    try
    {
        int rptId = GetReportIDForName(report);

        DataTable UnitReportPairEmailValsDT = new DataTable();
        string qry = string.Format(SQL.UnitReportPairEmailQuery, unit, rptId);
        UnitReportPairEmailValsDT = SQL.ExecuteSQLReturnDataTable(
            qry,
            CommandType.Text,
            null
            );

        List<String> emailAddresses = UnitReportPairEmailValsDT
                     .AsEnumerable()
                     .Select(row => row.Field<string>("EmailAddr"))
                     .ToList();

        model.UnitReportPairEmailVals = emailAddresses;
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    return Json(model, JsonRequestBehavior.AllowGet);
}

// View (jquery)
var model = JSON.stringify({ unit: unitval, report: rptval });
    $.ajax({
        type: 'GET',
            url: '@Url.Action("GetUnitReportPairEmailAddresses", 
    "UnitReportPair")',
            data: { unit: unitval, report: rptval }, // data: model,
            contentType: 'application/json',
            cache: false,
            success: function (result) {
                alert('success');
                alert(result.data);
            },
            error: function () {
                alert('failure');
            }
        });

UPDATE 2

Once I pieced together several different answers to this and related questions, I wrote up a tip on how to do this here.

Asef Hossini
  • 655
  • 8
  • 11
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 3
    Use your browser tools (the Network tab) to inspect the response to the call. It will give you the details of the error. –  Apr 21 '16 at 23:29
  • 1
    add a breakpoint in your controller action, hit play, trigger the ajax request and step through the action. – Lars Anundskås Apr 21 '16 at 23:37
  • 1
    NOTE: the `responseText` shown in the request is HTML likely meaning that what got passed back to you AJAX call is the HTML for the error page in your app. As @LarsAnundskås noted you really need to catch this error at the server (while debugging) to get to the bottom of it. – David Tansey Apr 22 '16 at 01:10

3 Answers3

1
contentType: "application/json; charset=utf-8",
dataType: "json",

Also check network tab of chrome dev tools and inspect that your request generates valid GET URL with your data as well as investigate the response output. -- Hope Helps

Syed Ekram Uddin
  • 2,907
  • 2
  • 29
  • 33
1

Your browser can help you debug the sent and received messages. I think it's F12 in all browsers (excluding Safari) and there should be a tab for Network that catches and displays all the traffic.

Your IDE, with the appropriate debugging setup, can step you through the code and catch most server errors (Visual Studios has a built in debugger, xdebug can be used for PHP) .

Finally, you can download something like Fiddler http://www.telerik.com/fiddler that will you give you more details at the web level, similar to the browser debugging tools. This can be very useful for evaluating the data that is sent and received through AJAX calls.

xCRKx TyPHooN
  • 808
  • 5
  • 18
  • Handle your HttpRequests with a HttpFilter and if it catchs any error return it to your ajax method or any view. Research some this topic dont ask me – Hamit YILDIRIM Apr 22 '16 at 15:39
1

Since it is a GET action method, if you are returning json data, you should explicitly specify JsonRequestBehaviour.AllowGet.

This should fix it.

return Json(model,JsonRequestBehavior.AllowGet);

If your action method is decorated with [HttpPost] and you are making a POST call, you do not need to explicitly specify it, return Json(model) will work fine.

Here is a more detailed explanation about the same.

Community
  • 1
  • 1
Shyju
  • 214,206
  • 104
  • 411
  • 497