首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JQuery处理错误时

使用JQuery处理错误时
EN

Stack Overflow用户
提问于 2014-09-02 08:01:41
回答 2查看 2.9K关注 0票数 2

我正在使用JQuery 什么时候。语法如下所示:

代码语言:javascript
复制
$.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);

});

我的问题

  1. 当对服务器的一个调用导致异常(失败场景)或任何其他场景的错误处理时,我如何进行错误处理?
  2. 既然我在这里提出Ajax请求,那么如何定义Ajax请求的超时时间呢?
EN

回答 2

Stack Overflow用户

发布于 2014-09-02 08:06:18

deferred.then( doneCallbacks,failCallbacks )可以接受类似的故障过滤器

代码语言:javascript
复制
$.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
});

要设置超时,可以使用timeout选项。

您可以在全局范围内使用它,比如

代码语言:javascript
复制
jQuery.ajaxSetup({
    timeout: 5000
})

或者使用$.ajax()代替短版本的$.get()timeout选项。

票数 3
EN

Stack Overflow用户

发布于 2014-09-02 08:08:05

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

代码语言:javascript
复制
    $.ajax({
          url: "file.php",
          type: "POST",
          async: false,
          success: function(data) {

          }
    });

呼叫是同步的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25618593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档