When jquery Ajax sends a request to the server, the server has an exception, such as: 400, 403, 404, 500 and other exceptions, the server will be abnormal response to the client, the AJAX can get exception information and processing, but at this time we generally jump to the exception code corresponding to the exception page, Show and deal with the anomaly.
First, send an AJAX request:
$.ajax ({
Type: ' POST ',
Url:url,
Data:data,
Success:success,
Datatype:datatype
});
Then, the service has an exception, and the corresponding exception code responds to the client:
Response.senderror (404);
return false;
Finally, the handling code of the exception is introduced into each page as a public resource, realizing the function of unifying presentation and handling exception, using jquery's Ajaxerror event:
事件说明:
事件使用代码:
$(function(){The. Ajaxerror event navigates to the Document object, and an AJAX request exception occurs for all elements within thewill bubble to the Document object's Ajaxerror eventLine processing, the Ajax method has error, first processing error, and then bubbling to the error $ (document) here. Ajaxerror (The uniform handler for all AJAX request exceptions, processing function (event,xhr,options,exc) { if (xhr.status = = ' undefined ') { Return } switch (xhr.status) { case 403: //Unauthorized exception alert ("system denied: You do not have access rights. "); Break ; case 404:alert ("The resource you are accessing does not exist. "); Break ;} } ); });
Or define a global Ajaxerror method: (If there is error handling in the page Ajax request, do not execute here)
$(function() {$.ajaxsetup ({error:function(Request) {if(!request | | request.status = = 0) return; if(Request.status = = 318) {// varInreload = Request.getresponseheader (' in-reload ')); if(Inreload = = 1) { varCheck = Confirm ("This session has timed out, click ' OK ', log back in"); if(check) {top.location.href= decodeURI (Top.location.href.split (';') [0]); } return; }}}, complete:function(Request, status) {Try { varInfefresh = Request.getresponseheader (' Is-refresh ')); varInlogin = Request.getresponseheader (' In-login ')); varRefreshurl = Request.getresponseheader (' Refresh-url ')); if(Infefresh = = ' 1 ' | | inlogin = = ' 1 ') { if(Refreshurl = =NULL|| Refreshurl = = "') {window.location.reload (); } Else { Try{Refreshurl=decodeURI (Refreshurl); Top.location.href=Refreshurl; } Catch(e) {window.location.reload (); } } } } Catch(e) {//the background is not set Responseheader does not do processing } } });});
For non-AJAX request exceptions, go directly to Web. XML to handle:
<!-- Error page --><error-page> <exception-type>java.lang.Throwable</exception-type> <location>/WEB-INF/views/error/500.jsp</location></error-page><error-page> <error-code>500</error-code> <location>/WEB-INF/views/error/500.jsp</location></error-page><error-page> <error-code>404</error-code> <location>/WEB-INF/views/error/404.jsp</location></error-page><error-page> <error-code>403</error-code> <location>/WEB-INF/views/error/403.jsp</location></error-page><error-page> <error-code>400</error-code> <location>/WEB-INF/views/error/400.jsp</location></error-page>
The mechanism of uniform handling of exception error messages during jquery AJAX requests