Jquery 如何从多个ajax调用中获取数据?

Jquery 如何从多个ajax调用中获取数据?,jquery,ajax,Jquery,Ajax,在这里,我从fetchBooks()调用fetchbookAllNew()方法,但是这个ajax加载程序工作不正常。当fetchBooks被称为ajax加载程序时,我需要的是显示所有数据,直到我将在函数中获取所有数据。完成函数 $scope.fetchBooks = function(){ $(".loader").show(); $.when( fetchbookAllNew("all"), fe

在这里,我从
fetchBooks()
调用
fetchbookAllNew()
方法,但是这个ajax加载程序工作不正常。当
fetchBooks
被称为ajax加载程序时,我需要的是显示所有数据,直到我将在
函数中获取所有数据。完成
函数

$scope.fetchBooks = function(){
        $(".loader").show();
        $.when(

                fetchbookAllNew("all"),
                fetchbookAllNew("epub"),
                fetchbookAllNew("collection"),
                fetchbookAllNew("video"),

              ).done(function(publishedAll, publishedEpub, publishedColl,publishedVideo){

                  $scope.allkitabooBooks = publishedAll;
                $scope.allEpubBooks =publishedEpub;
                $scope.allcollectionbooks = publishedColl;
                $scope.allvideosbooks = publishedVideo;

                $(".loader").fadeOut("slow");
              });


    };

    var fetchbookAllNew = function(status){
            var books = undefined;

             $.ajax({
                  url:'/booksList', // Dynamically uploads the files which is chosen.  
                  type: 'GET',

                  headers : {
                        'usertoken' : $rootScope.userDetails.userToken,
                        'status':status
                            },
                  cache: false,
                  async: false,
                  processData: false,           // Don't process the files
                  contentType:'application/json',   // Setting content type to "application/octet-stream"/"undefined" as jQuery will tell the server its not query string.

                  success: function (data) {
                      books=data;

                  },
                  error: function (data) {

                  }
              });
             return books;

        };

这应该可以做到,您需要返回ajax承诺,而不是结果。
done()
为您解析结果


永远不要使用
ajax:false
。它破坏了promises和ajax的目标。

您需要
返回$.ajax
,而不是响应。我需要每个请求的响应,以根据状态显示数据。还需要reomve ajax:false。这是错误的解决方案。所以这里至少有3个不同的问题。嗨,它适用于对ajax函数的单次调用:fetchbookAllNew(),但如果在超时时有更多的调用,那不是js。如果超时,则说明服务器占用的时间太长,或者出现锁定问题hi@ShitalKhatal。通常我不会这么说,但因为你的新朋友。表示感谢的最好方式是接受和/或投票。这也值得一看。很高兴这有帮助。
$scope.fetchBooks = function(){
        $(".loader").show();
        $.when(
                fetchbookAllNew("all"),
                fetchbookAllNew("epub"),
                fetchbookAllNew("collection"),
                fetchbookAllNew("video"),
              ).done(function(publishedAll, publishedEpub, publishedColl,publishedVideo){
                // Each argument is an array with the following 
                //                   structure: [ data, statusText, jqXHR ]
                //so [0] is data
                $scope.allkitabooBooks = publishedAll[0];
                $scope.allEpubBooks =publishedEpub.data[0];
                $scope.allcollectionbooks = publishedColl.data[0];
                $scope.allvideosbooks = publishedVideo.data[0];
                $(".loader").fadeOut("slow");
              });
    };

    var fetchbookAllNew = function(status){
             return $.ajax({
                  url:'/booksList', // Dynamically uploads the files which is chosen.  
                  type: 'GET',
                  headers : {
                        'usertoken' : $rootScope.userDetails.userToken,
                        'status':status
                            },
                  cache: false,
                  //async: false,//don't EVER do this
                  processData: false,           // Don't process the files
                  contentType:'application/json',   // Setting content type to "application/octet-stream"/"undefined" as jQuery will tell the server its not query string.   
              });
        };