Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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}]

share|improve this question
    
Change the order of status and data on function header. –  Beterraba Jan 9 at 18:53
add comment

1 Answer

up vote 3 down vote accepted

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
});
share|improve this answer
    
Wow... I can't believe I was that dumb. I had a slinking suspicion I was missing something trivial. Thank you for the answer! I'm new to this whole thing and it was a great help. –  AndyVan Jan 13 at 16:34
1  
@AndyVan no problem! Sometimes, it's best just to take a little break, then look over the docs again :) –  m59 Jan 13 at 16:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.