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

喜夏-厌秋 提交于 2019-12-12 03:28:45

问题


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:

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.


回答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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/36782122/how-can-i-determine-the-cause-of-the-internal-server-error-resulting-from-my-aja

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!