Wednesday, 15 April 2015

asp.net mvc 3 - Displaying db exception from mvc controller to my ajax call? -


The best approach to demonstrating the exception of clients (AJAX calls) is not sure? Will I have a try / hold in the controller? Currently, do I get a general exception in my AJAX call?

Controller:

  // GET: Public agency JS grant for GetAgencyGrants (int agency id) {auditDAL ad = new AuditDAL (); Var Grant = ad.GetAgencyGrants (Agency Id); // Exceptions come back here. Jason (grant.Tolist ()); } Ajax call:  
  function GetAuditTypes (container) {$ .ajax ({url: '/ AMS / audit / GetAidotaps', type:' Post ', success: work (results) {$ .eight (results, work (i, items) {container.apend ($ (' & lt; option / & gt; ') text (result [i] .Audit_Type_Desc) .attr ('value', result [i] .Audit_Type_ID);}};}, error: function (xhr, err) {warning ("GetAuditTypes not back:" + formatAerreas message (excerpt, mistake))}} ); }   

Formatting function:

  // format errors returned the function format. Internal message (jqXHR, exception) {if (jqXHR.status == = 0) {return ('not connected. \ NPlease confirm your network connection.'); } And if (jqXHR.status == 404) {return ('The requested page was not found.' [404] '); } And if (jqXHR.status == 500) {return ('internal server error [500].'); } And if (exception === 'parser') {return ('requested Jason pars unsuccessful.'); } And if (exception === 'timeout') {return ('timeout error.'); } And if (exception === 'abortion') {Return ('Ajax Request Rejected.'); } And {return ('unsolicited error. \ N' + jqXHR.responseText); }}   

I'm getting an internal server error instead of stored proc (real exception).

AJAX error handles callback function http Errors (but not specific server errors) This will not show this special error message. Change your code like this:

  Public JS Reits GetAgencyGrants (Int. Agency ID) {Audit Dual Ad = New Audital (); Try {var grants = ad.GetAgencyGrants (Agency ID); // Exceptions come back here. Jason (grant.Tolist ()); } Hold (exception e) {this return.Json (e.Message); }}   

and change you'r javascript code to:

  $ Ajax ({url: '/ AMS / Audit / GetAid Type', Type: 'Post', Success: Function (Results) {if (Results Example Array) {$ .each (Results, Work (i, Item) { Container.append ($ ('option / & gt;'). Text (result [I] .Audit_Type_Desc) .attr ('value', result [i]. Audit_Type_ID));});} and {/ / Exception occurred / Somethings with result (error message included)}}});    

No comments:

Post a Comment