jquery - 为什么需要 jQuery AJAX #then 或 #done 或 #fail 方法?

标签 jquery ajax

在jQuery的aJax方法中,传入的对象包括成功和错误处理。如果 ajax 请求参数已经可以处理这些问题,为什么您还需要使用 #then 或 #done 或 #fail 等方法呢?

最佳答案

successerror 不能用于传递由 $.ajax 返回的 promise 链中的 promise

您无法像在 then() 中那样向这些方法返回任何内容。

考虑一系列 Ajax 请求,这些请求必须全部完成才能执行其他代码。

$.getJSON(url)
  .then(function(resp1) {
     // this request won't run until previous one completed
    return $.getJSON(resp1.urlValue).done(function(resp2) {
          // can do things in individual request done also
    });
  }).then(function(resp2) {

     return $.getJSON(resp2.urlValue, function(resp3){
               // or do something in success callback for this request
            });

  }).then(function() {
     // do something here now that all the requests have resolved
  }).fail(function() {
    alert('I fire if any of the above fail');
  });

此链无法使用成功回调来工作

关于jquery - 为什么需要 jQuery AJAX #then 或 #done 或 #fail 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35615872/

相关文章:

javascript - jquery背景颜色在滚动时淡入

java - GWT + symfony2,我疯了吗?

javascript - Ajax 成功响应警报不起作用 C#

javascript - Prototype js 如何使定期更新器根据ajax响应隐藏类的元素

javascript - 外部 Ajax : All at Once, 根据需要,还是其他?

javascript - 如何在 $(document).ready 之前将 jQuery.js 包含在自己的 .js 中?

jquery - 时钟选择器 : How to prevent click on input to open clockpicker

javascript - 如何从复选框标签中的 anchor 显示模态

php - 从 MySql 填充选项选择菜单

javascript - JSON 文件中的特殊字符转换