Javascript 使用JQuery时出错处理

Javascript 使用JQuery时出错处理,javascript,jquery,jquery-deferred,Javascript,Jquery,Jquery Deferred,我正在使用JQuery。语法如下所示: $.when( // Get the HTML $.get("/feature/", function(html) { globalStore.html = html; }), // Get the CSS $.get("/assets/feature.css", function(css) { globalStore.css = css; }), // Get the JS $.getScript("/

我正在使用JQuery。语法如下所示:

$.when(
  // Get the HTML
  $.get("/feature/", function(html) {
    globalStore.html = html;
  }),

  // Get the CSS
  $.get("/assets/feature.css", function(css) {
    globalStore.css = css;
  }),

  // Get the JS
  $.getScript("/assets/feature.js")

).then(function() {

  // Add CSS to page
  $("<style />").html(globalStore.css).appendTo("head");

  // Add HTML to page
  $("body").append(globalStore.html);

});
$。什么时候(
//获取HTML
$.get(“/feature/”,函数(html){
globalStore.html=html;
}),
//获取CSS
$.get(“/assets/feature.css”,函数(css){
globalStore.css=css;
}),
//获取JS
$.getScript(“/assets/feature.js”)
).然后(函数(){
//将CSS添加到页面
$(“”).html(globalStore.css).appendTo(“head”);
//将HTML添加到页面
$(“body”).append(globalStore.html);
});
我的问题

  • 当对服务器的某个调用导致异常(故障场景)或任何其他场景的错误处理时,我如何进行错误处理
  • 既然我在这里发出Ajax请求,那么如何定义Ajax请求的超时时间呢
  • 可以采取类似的故障过滤器

    $.when(
      // Get the HTML
      $.get("/feature/", function(html) {
        globalStore.html = html;
      }),
    
      // Get the CSS
      $.get("/assets/feature.css", function(css) {
        globalStore.css = css;
      }),
    
      // Get the JS
      $.getScript("/assets/feature.js")
    
    ).then(function() {
    
      // Add CSS to page
      $("<style />").html(globalStore.css).appendTo("head");
    
      // Add HTML to page
      $("body").append(globalStore.html);
    
    }, function(){
        //there is an exception in the request
    });
    

    或者使用
    $.ajax()
    而不是短版本
    $.get()
    超时
    选项

    我认为这是因为调用是异步的。使用时:

        $.ajax({
              url: "file.php",
              type: "POST",
              async: false,
              success: function(data) {
    
              }
        });
    

    呼叫是同步的

    您检查了吗。错误???谢谢您的快速回复。我需要为个人请求应用超时时间
    JQuery.ajaxSetup
    在这种情况下没有帮助。@SharpCoder在这种情况下,您可以使用我建议的第二个选项,如果您
    .done
    而不是
    ,那么
    ?如何处理错误?Doc没有提到任何事情:
        $.ajax({
              url: "file.php",
              type: "POST",
              async: false,
              success: function(data) {
    
              }
        });