Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS $http get data object displays status code instead of response

I have mulled over this for days and can still not figure out what I'm doing incorrectly so any ideas or even shots in the dark are appreciated. I am trying to display the response from a rest service to the user using the using the AngularJS $http get method, but when I print the data object to the console, I consistently receive the number 200 (I'm fairly certain it is giving me the status code). I hit success every time and, upon sending the request, the Chrome debug tool shows me the response with all the correct data. I just can't seem to get it to appear in a variable for display. Let me know if you think of anything! Thanks!

My 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;
};

And the service response:

[{"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}]

like image 878
AndyVan Avatar asked Jan 09 '14 18:01

AndyVan


1 Answers

You have the arguments in the wrong order. It should be: success(function(data, status, headers, config)

See the docs here (click).

Also, the .then() method is generally preferred. If you switch to that, you would access the data like this:

.then(function(response) {
  var data = response.data;
  var status = response.status;
  //etc
});
like image 72
m59 Avatar answered Oct 10 '22 07:10

m59