2016-08-26 27 views
2

這裏我打電話fetchbookAllNew()方法從fetchBooks()但與此ajax加載程序不能正常工作。當我想要fetchBooks被稱爲ajax加載器時,我應該顯示,直到所有的數據,我將獲得.done函數。如何從多個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; 

     }; 
+0

你需要'返回$ .ajax',而不是響應。 – Liam

+0

我需要每個請求的響應來顯示數據根據狀態 –

+0

也rejve ajax:false。這是錯誤的解決方案。所以你在這裏至少有三個不同的問題。 – Liam

回答

1

這應該做到這一點,您需要返回ajax承諾,而不是結果。 done()爲您解決了結果。 More info

$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. 
       }); 
     }; 

永遠不要ajax:false。它擊敗了promise和ajax的對象。

+0

嗨,它的工作單個調用ajax函數:fetchbookAllNew(),但如果有更多的電話,當我超時。 –

+0

這不是js。如果你得到一個超時然後你的服務器花費太長時間,或者你有一個鎖定問題 – Liam

+0

Thanx Alot..its working :) –