2014-01-09 43 views
5

我已經仔細考慮了這些日子,仍然無法弄清楚我做錯了什麼,所以任何想法或甚至在黑暗中的鏡頭都被讚賞。我試圖使用使用AngularJS $ http get方法顯示從休息服務對用戶的響應,但是當我將數據對象打印到控制檯時,我始終收到數字200(我相當肯定它給了我是狀態碼)。我每次都獲得成功,發送請求後,Chrome調試工具會向我顯示所有正確數據的響應。我似乎無法讓它出現在顯示的變量中。讓我知道,如果你想到任何東西!謝謝!

我的javascript:

$scope.resendDestinations = []; 
$scope.resendDestGet = function() { 
    var omtTypeCodeString = ''; 

    for(var i = 0; i < $scope.mySelections.length; i++){ 
     if(omtTypeCodeString == ''){ 
      omtTypeCodeString = $scope.mySelections[i].orderHeader.omtOrderTypeCode; 
     } 
     else{ 
      omtTypeCodeString = omtTypeCodeString + ',' + $scope.mySelections[i].orderHeader.omtOrderTypeCode; 
     } 
    } 

    $http({ 
     method: 'GET', 
     url: restService.pom + //service url, 
     respondType: 'json', 
     headers: { 
      'Accept': 'application/json', 
      'Content-Type': 'application/json', 
      'Access-Control-Allow-Credentials': true 
     }, 
     params: { 
      orderTypeCode: omtTypeCodeString, 
      transactionCode: 3 
      } 
     }).success(function (status, data, response, header) { 
      console.log("Success!"); 
      //TODO see if this is being used... has to be 
      status = parseInt(status); 
      $scope.resendDestinations = data.multipleOrders; 
      if (status == 200 && $scope.resendDestinations.length == 0) { 
       $scope.bigAlert.title = 'Error', 
       $scope.bigAlert.header = 'Search Error'; 
       $scope.bigAlert.content = 'Current search parameters do not match any results.'; 
       $scope.showBigAlert(); 
      } 
      else{ 
       $scope.resendDestinations = data; 
       console.log("Data DestinationList here: "); 
       console.log($scope.resendDestinations); 
       console.log(data.multipleOrders); 
       console.log(data); 
      } 
      $scope.isSearching = false; 
     }).error(function (response, data, status, header) { 
      //Do error things 
     }); 
    return $scope.resendDestinations; 
}; 

,服務響應:

[{"destCode":3,"destDescr":"Repository","attributes":null},{"destCode":4,"destDescr":"Pipeline","attributes":null},{"destCode":1,"destDescr":"Processor","attributes":null},{"destCode":2,"destDescr":"DEW","attributes":null}, {"destCode":7,"destDescr":"Management System","attributes":null}, {"destCode":8,"destDescr":"Source","attributes":null}]

+0

更改函數標頭上狀態和數據的順序。 – Beterraba

回答

14

你以錯誤的順序的參數。它應該是:success(function(data, status, headers, config)

參見docs here (click).

另外,.then()方法通常是優選的。如果切換到此,您將訪問如下數據:

.then(function(response) { 
    var data = response.data; 
    var status = response.status; 
    //etc 
}); 
+0

哇......我不敢相信我是那個笨蛋。我有一種懷疑我錯過了一些微不足道的懷疑。謝謝你的回答!我對這件事情很陌生,對我很有幫助。 – AndyVan

+1

@AndyVan沒問題!有時候,最好稍微休息一下,然後再查看文檔:) – m59

+1

使用'.then()'和'.success()'和'.error()' –