Bootstrap模态远程源error handling
我们使用Bootstrap Modal window
显示通过远程源加载的一些html。 我们通过Bootstrap
文档中推荐的方式执行此操作,方法是使用remote
选项并将其传递给url
。 (如此处所述)
例如:
$('#id').modal({remote:'index.html'});
我的问题:在index.html不可用的情况下是否可以处理错误?
我在文档中没有看到任何答案。
我知道这应该很少发生,但是如果有人连接缓慢或参差不齐,我宁愿向他们展示一个错误,而不是只用一个空模态挂起。
您可能希望在应用程序中实现全局Ajaxerror handling程序,这将附加到执行的每个Ajax请求,实现将如下所示:
$( document ).ajaxError(function( event, jqxhr, settings, exception ) { //Since this handler is attach to all ajax requests we can differentiate by the settings used to build the request if ( settings.url == "index.html" ) { //Handle error } });
您可以在此处阅读有关全局Ajax处理程序的更多信息
目前,Github repo( /js/modal.js )在模态插件定义中包含此片段:
… if (this.options.remote) this.$element.load(this.options.remote) …
这表示没有使用回调,请求的结果直接分配给正在处理的dom元素。
来自docs jQuery.load :
此方法是从服务器获取数据的最简单方法。 它大致相当于$ .get(url,data,success),除了它是一个方法而不是全局函数,它有一个隐式回调函数。 当检测到成功响应时(即当textStatus为“success”或“notmodified”时),。load()将匹配元素的HTML内容设置为返回的数据。
稍后在doc中有一个代码snippt,它描述了如何使用load
检测故障:
$("#success").load("/not-here.php", function(response, status, xhr) { if (status == "error") { var msg = "Sorry but there was an error: "; $("#error").html(msg + xhr.status + " " + xhr.statusText); } });
Twitter团队似乎选择不处理错误。
也许现在是时候开始一个问题,看起来像“移动第一”的图书馆想要优雅地处理这种事情;-) https://github.com/twbs/bootstrap/issues