ajmajmajma
ajmajmajma

Reputation: 14216

Sending data in angular $http post

I seem to be having some difficulty sending a val of a selected of a selected item into a new %http post in angular.

I am doing the following:

$scope.scope1Change = function() {
          //Build URL based on selection
        $http({
            method: 'POST',
            url: '/listAreasByScope', 
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            data: { id: $scope.scope1.id }
        })
        .success(function(data){
            $scope.array2 = data.scopes;
        });
    };

Where $scope.scope1.id is the val (which is the id im trying to send) of the previous drop down in which this event is triggered. $scope.scope1.id seems to be storing just fine when I console.log it however when I look at the formdata in the dev console it is formatting very oddly like so -

 {"id":2}: 

It looks like it is recognizing the whole first item as the key. Any way around this? I have tried various ideas with no success.

Thanks!

Upvotes: 0

Views: 187

Answers (1)

dss
dss

Reputation: 470

You need to use params instead of data. AngularJs docs

$scope.scope1Change = function() {
      //Build URL based on selection
    $http({
        method: 'POST',
        url: '/listAreasByScope', 
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        params: { id: $scope.scope1.id }
    })
    .success(function(data){
        $scope.array2 = data.scopes;
    });
};

Upvotes: 1

Related Questions