我想在有
AJAX错误时提醒用户.你可以通过$.ajaxSetup:
$.ajaxSetup({ error: function() { alert("OOPS!") } });
But the jQuery docs recommend against this:
Note: Global callback functions should
be set with their respective global
Ajax event handler
methods-.ajaxStart(),.ajaxStop(),
.ajaxComplete(),.ajaxError(),
.ajaxSuccess(),.ajaxSend()-rather
than within the settings object for
$.ajaxSetup().
所以我想我应该这样做:
$("#some_random_div").ajaxError(function() { alert("OOPS!") });
这对我来说没有意义:AJAX错误回调与我的应用程序整体相关,而不是特定的页面元素.那么为什么回调函数与单个页面元素相关联?
那么为什么jQuery文档建议使用看起来像一个不太可读的方法,哪种方法最好?
解决方法
典型的用法是绑定ajaxError的特定页面元素是要显示错误消息的div;也许是您页面顶部的“flash”消息div.这样你可以直接访问该div来附加你的消息.
但是,如果您不使用特定的DOM元素作为消息显示区域,则可以将其绑定到$(document)或其他一些这样的全局元素.