B. Clay Shannon B. Clay Shannon - 7 months ago 10
Javascript Question

How can I determine the cause of the Internal Server Error resulting from my Ajax call?

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 sugged by nurdyguy here [Why is my AJAXoned Action's return not being seen as successful by the caller?:

$.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.

Answer

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.

Comments