Show correct error messages using jQuery AJAX

Currently, if I have an error with ajax request, for example, with submitting a form (I am using ASP.NET MVC 3), it will show something like "Internal server error".

But if I feed without using Ajax, then .net will show the actual server error.

Any ideas on how I could get an error to show? Therefore, my users can report a problem instead of saying "internal server error"

thanks

+7
source share
2 answers

Although I would not recommend showing the user the error number with the actual error message, so you can do this by adding the error callback

 error: function(xhr,err){ alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status); alert("responseText: "+xhr.responseText); } 

xhr is an XmlHttpRequest.

readyState : 1: download, 2: download, 3: interactive, 4: full.

status - HTTP status number, i.e. 404: not found, 500: server error, 200: ok.

responseText is the response from the server - it can be text or JSON from the web service or HTML from the web server.

+23
source

This code will replace the current page content with the error returned from the ajax request.

 jQuery(document).ajaxError(function (event, request, settings) { if (request.status === 500) { $(document.body).html(request.responseText); } else if (request.status === 401) { // redirect to login, 401 means session is expired. window.location.href = "login page url"; } }); 
+6
source

All Articles