如何最好地处理由用户单击链接导致的jquery ajax调用期间的错误

我正在逐步增强我的网站的ajax-tivity(TM),并且最近添加了一些非常基本的error handling。 我的调用是通过jquery .ajax()方法进行的,我用show_error_box(消息)函数向用户显示错误我通过返回脚本来处理所有的ajax,因此服务器端validation很容易 – 我只是发回一个调用show_error_box。 对于其他错误,我只需将以下代码放入我的.ajax调用中

error: function(xhr, status, error) { show_error_box("There has been an error communicating with the server
The field has not been updated
Please check your internet connection"); }

我发现这很好地解决了大多数问题(没有连接,超时等),但当用户在ajax调用仍在进行时点击链接时,它的处理能力很差。 错误框开始显示,然后突然将用户带到新页面。

处理这种情况的更好方法是什么? 理想情况下,我会显示一个非侵入式错误消息(例如’请等到更新完成’)并忽略链接上的点击。 有关如何最好地实现这一点的任何建议?

编辑:

我通过暂时禁用所有链接然后重新启用它来解决这个问题;

 $.ajaxSetup({ beforeSend: function() {disable_links()}, complete: function() {enable_links()}, timeout: 15000 }); function disable_links() { $('a').each(function() { if ($(this).attr('href')) { $(this).data('href', $(this).attr('href')).removeAttr('href'); } }) } function enable_links() { $('a').each(function() { $(this).attr('href', $(this).data('href')); }) } 

也许你可以使用BlockUI jQuery插件。 这将阻止用户在当前AJAX请求返回之前单击链接:

 $(document).ajaxStart($.blockUI).ajaxStop($.unblockUI);