-1

I have this simple array and I need to make a post request to a backend controller but every time I get a empty array []

Here my html:

<div class="input-container" ng-repeat="lang in languages">
    <label for="tagName">{{:: 'tagName' | translate }}</label>
    <input placeholder="{{:: 'tagName' | translate }}" id="tagName" type="text" ng-model="translations[lang]">
</div>

Here my JS:

$scope.createNewTag = function () {
    console.log($scope.translations);
    var data = $scope.translations;

    var method = 'post';
    var url = './tags';
    $http.post(url,data);      
};

The console.log($scope.translations); print: [en:"hello", it:"ciao"] but when I see in Chrome console the request playload is empty and in my controller I get an empty array.

What am I missing?

Christian Giupponi
  • 7,408
  • 11
  • 68
  • 113

1 Answers1

0

you forgot to resolve the promise of the post call, if you don't you won't be able to handle the response.

$http.post('/someUrl', data)
 .then(function(resp){
   //success
 }), function(err){
   //catch exception
 });

https://docs.angularjs.org/api/ng/service/$http

Regarding the empty payload i'd look through the code to check if there is any http interceptors that manipulate the request before it triggers, maybe you changed the content-type header there and Angular is not able to serialize the data anymore.

maybe this could help you:

Angularjs $http POST request empty array

EDIT: i see that you're passing an array as an argument, but $http specs states that it must be a key-value pair object, try wrapping your array inside an object.

var params = {
  data: $scope.translations
}
$http.post('/someUrl', params)
Community
  • 1
  • 1
Karim
  • 8,454
  • 3
  • 25
  • 33