Bootstrap模态远程源错误处理

nos*_*omo 12 javascript jquery twitter-bootstrap

我们Bootstrap Modal window用来显示一些通过远程源加载的html.我们通过Bootstrap文档中推荐的方式,通过使用选项remote并传递它来完成此操作url.(如所描述的在这里)

例如:

$('#id').modal({remote:'index.html'});
Run Code Online (Sandbox Code Playgroud)

我的问题:在index.html不可用的情况下是否可以处理错误?

我在文档中没有看到任何答案.

我知道这应该很少发生,但是如果有人连接缓慢或参差不齐,我宁愿向他们展示一个错误,而不是只挂一个空模态.

koa*_*dev 6

您可能希望在应用程序中实现全局Ajax错误处理程序,这将附加到执行的每个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
  }
});
Run Code Online (Sandbox Code Playgroud)

您可以在此处阅读有关全局Ajax处理程序的更多信息


Mar*_*Fox 5

目前,Github存储库(/js/modal.js)在模式插件定义中包含以下片段:

…
if (this.options.remote) this.$element.load(this.options.remote)
…
Run Code Online (Sandbox Code Playgroud)

这表示没有使用回调,请求的结果直接分配给正在处理的dom元素。

从文档jQuery.load

此方法是从服务器获取数据的最简单方法。它与$ .get(url,data,success)大致等效,除了它是方法而不是全局函数且具有隐式回调函数。当检测到成功的响应时(即,当textStatus为“成功”或“未修改”时),. load()将匹配元素的HTML内容设置为返回的数据。

稍后在文档中有一段代码片段,描述了如何使用以下命令检测故障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);
  }
});
Run Code Online (Sandbox Code Playgroud)

Twitter团队似乎选择处理该错误。

也许是时候开始发布问题了,似乎“移动优先”库想优雅地处理这种事情;-) https://github.com/twbs/bootstrap/issues