jquery - How can I determine the cause of the Internal Server Error resulting from my Ajax call? -
in asp.net mvc app, 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 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 "failed" alert, , "result" object has no end of data; here's tip of iceberg:
it seems key thing status of 500, , status text of "internal server error"
so commented out 2 lines , added in "debugger" in stead (as sugged nurdyguy here [why ajaxoned action's return not being seen successful caller?:
$.ajax({ . . . error: function (result) { debugger; //alert('failed'); //alert(result); } });
...yet don't see gets me. putting ajax call through paces shows nothing in browser; using chrome devtools, can step it, once "debugger" line, doesn't me favors. f11 line nothing.
so how can bottom of what's causing 500/internal server error?
update
since have commented out "json" lines jquery ajax call ("var model = json.stringify({})" , "contenttype: 'application/json'"), should change controller return type jsonresult actionresult , "return view(model)"?
update 2
i getting "success" ajax method this, incorporating several suggestions y'all , changing model datatable member 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 pieced several different answers , related questions, wrote tip on how here.
since action method, if returning json data, should explicitly specify jsonrequestbehaviour.allowget
.
this should fix it.
return json(model,jsonrequestbehavior.allowget);
if action method decorated [httppost]
, making post call, not need explicitly specify it, return json(model)
work fine.
here more detailed explanation same.
Comments
Post a Comment