2013-10-18 11 views
13

मैं अपने कोणीय नियंत्रक में http शीर्षलेखों तक पहुंचने का प्रयास कर रहा हूं लेकिन मुझे अपरिभाषित हो रहा है। इसके अलावा, मैं अपने कोणीय सेवा में हेडर प्रतिक्रिया देखने में सक्षम हूं जो मेरे नियंत्रक में प्रतिबिंबित नहीं कर रहा है। क्या कोई मुझे बता सकता है कि मुझे क्या याद आ रहा है? कृपया नीचे दिए गए कोड देखें:एंगुलरजेएस - http शीर्षलेखों तक पहुंच

सेवा:

cmApp.service('supplierService', function ($http, $q) { 
    this.getSuppliers = function (orderByColumn, skipRows, takeRows) { 
     var deferred = $q.defer(); 
     $http({ 
      method: 'GET', 
      url: 'api/supplier', 
      params: { orderBy: orderByColumn, skip: skipRows, take: takeRows }, 
      timeout: 30000, 
      cache: false 
     }). 
     success(function (data, status, headers, config) { 
      // any required additional processing here    
      deferred.resolve(data, status, headers, config);    
     }). 
     error(function (data, status) { 
      deferred.reject(data, status, headers, config); 
     }); 
     return deferred.promise;   
    } 

नियंत्रक:

supplierService.getSuppliers($scope.orderby, $scope.skip, $scope.take) 
     .then(function (data, status, headers, config) { 
      **//getting undefined here.** 
      $scope.totalRecords = parseInt(headers('X-TotalRowCount'));     
      $scope.suppliers = data; 
     }, function (error) { 
      // error handling here 
     }); 
+1

कृपया $ httpProvider देखें। अपने हेडर को नियंत्रित करने के लिए इसका इस्तेमाल करें। – kroonwijk

उत्तर

18

मैं अपने आप को द्वारा समाधान मिल गया है। मुझे बस इतना करना है कि एक सरणी बनाएं और उन सभी मानों को उसी & पर कंट्रोलर पर वापस लाएं। कृपया नीचे अद्यतन कोड देखें:

सेवा:

cmApp.service('supplierService', function ($http, $q) { 
    this.getSuppliers = function (orderByColumn, skipRows, takeRows) { 
     var deferred = $q.defer(); 
     $http({ 
      method: 'GET', 
      url: 'api/supplier', 
      params: { orderBy: orderByColumn, skip: skipRows, take: takeRows }, 
      timeout: 30000, 
      cache: false 
     }). 
     success(function (data, status, headers, config) { 
      // any required additional processing here 
      var results = []; 
      results.data = data; 
      results.headers = headers(); 
      results.status = status; 
      results.config = config; 

      deferred.resolve(results);    
     }). 
     error(function (data, status) { 
      deferred.reject(data, status, headers, config); 
     }); 
     return deferred.promise;   
    } 

नियंत्रक:

supplierService.getSuppliers($scope.orderby, $scope.skip, $scope.take) 
      .then(function (response) {     
       $scope.suppliers = response.data; 
       $scope.totalRecords = parseInt(response.headers["x-totalrowcount"]);     
      }, function (error) { 
       // error handling here 
      }); 
+0

अच्छी तरह से काम करता है, जानकारी –

+0

के लिए धन्यवाद बहुत बहुत धन्यवाद। बहुत मदद की। – victorkurauchi

+0

नियंत्रक में, इस तरह होना चाहिए: $ scope.totalRecords = parseInt (response.headers ("x-totalrowcount")); पट्टे पर मेरे लिए यह काम। –

6

यह सवाल पुराना है, लेकिन $ http() एक वादा ही देता है। आप इसे अपनी सेवा से वापस कर सकते हैं, कोई नया वादा करने की आवश्यकता नहीं है। आप .success() और .error(), या उस मामले के लिए .then() का उपयोग करने के बाद भी ऐसा कर सकते हैं, वे चेनिंग रखते हैं।

2

कस्टम शीर्षलेख एक ही डोमेन में दिखाई देंगे। हालांकि, क्रॉसडोमेन की स्थिति के लिए, सर्वर को एक्सेस-कंट्रोल-एक्सपोज़-हेडर: X-Foo को क्रॉस-डोमेन प्रॉपर्टी के साथ * ... हेडर कस्टम हेडर दिखाई देने के लिए भेजना है।

1

मैंहेडर मेरी बाकी सेवा कीप्रतिक्रिया से टोकन और TokenExpiry समय तक पहुँचने के लिए है, तो मेरी $ rootScope में संग्रहीत किया था। यहां कोड का उपयोग किया गया है:

   $scope.Authenticate=function(){ 
        var EncDecUserPass=decodeURIComponent(encodeURIComponent($scope.LoggedUserName+':'+$scope.LoggedUserPassword)) ; 
        $http(
        {method: 'GET', 
        url: 'http://localhost:53256/api/Products/Authenticate', 
        cache: false, 
        headers:{'Authorization':'Basic '+window.btoa(EncDecUserPass)} 
        } 
        ).success(function(data, status, headers, config) { 
         //Here it goes 
         $rootScope.token=headers().token; 
         $rootScope.tokenExpirySec=headers().tokenexpiry; 
        }).error(function(data, status, headers, config) { 
        alert('Invalid User'); 
        }); 
       } 
+0

अच्छा दृष्टिकोण !! –

संबंधित मुद्दे