jQuery默认为ajax statusCode

前端之家收集整理的这篇文章主要介绍了jQuery默认为ajax statusCode前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
据我所知,使用ajax处理错误结果的一种可能性如下: @H_301_2@$.ajax({ url: someUrl,type: 'POST',success: function(data) {},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); } } });

或者使用statusCode以使其更易读:

@H_301_2@$.ajax({ url: someUrl,statusCode: { 200: function(data) { : : },401: function() { : : },: :

我的问题是:
是否可以使用statusCode并具有默认的fall-through?

解决方法

不在statusCode参数中.但是,如果你想抓住一切,那就是更好(更精简)的方式: @H_301_2@complete: function(jqXHR,textStatus) { switch (jqXHR.status) { case 200: alert("200 received! yay!"); break; case 404: alert("404 received! boo!"); break; default: alert("I don't know what I just got but it ain't good!"); } }

猜你在找的jQuery相关文章