jQuery Ajax错误处理功能
$( document ).ajaxError(function( event, request, settings ) {
$( "#msg" ).append( "
- Error requesting page " + settings.url + "
- " ); });
注意:如果在全局选项设置为false的情况下调用$ .ajax()或$ .ajaxSetup(),则不会触发.ajaxError()方法。
下面是一个较旧的函数,您可以从jqXHR对象获取状态。
$(function() { $.ajaxSetup({ error: function(jqXHR, exception) { if (jqXHR.status === 0) { alert('Not connect.n Verify Network.'); } else if (jqXHR.status == 404) { alert('Requested page not found. [404]'); } else if (jqXHR.status == 500) { alert('Internal Server Error [500].'); } else if (exception === 'parsererror') { alert('Requested JSON parse failed.'); } else if (exception === 'timeout') { alert('Time out error.'); } else if (exception === 'abort') { alert('Ajax request aborted.'); } else { alert('Uncaught Error.n' + jqXHR.responseText); } } }); });
From: https://www.sitepoint.com/jquery-ajax-error-handling-function/