0

I have a javascript variable which is an array of MyObjects. I can display this variable to the view with the following code:

    <tr ng-repeat="user in lala.users">
        <td>{{ user.firstName }}</td>
        <td>{{ user.lastName }}</td>
    </tr>

How can I send this to a server as json using post?

5
  • Why are you changing my tag from angular to angularjs? Commented Feb 6, 2018 at 14:53
  • $http.post is AngularJS syntax Commented Feb 6, 2018 at 14:57
  • Thanks mate! My bad. Commented Feb 6, 2018 at 14:58
  • 1
    @CapBaracudas have you tried: $http.post("http://localhost:8080/server/", {"data":lala.users}) ? Commented Feb 6, 2018 at 15:06
  • I am not sure why but it seems that I can just do $http.post('Url', lala.users) . Thanks though cause your answers works too. Commented Feb 6, 2018 at 16:27

2 Answers 2

0

you need to inject $http to you controller or Server or factory.

      $http.post('/saveusers', users).then(function(data) {
        $scope.message = data;
    });
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming it's a person object:

let postMessage = JSON.stringify({
  'person': {
    'firstName': this.user.firstName,
    'lastName': this.user.lastName
  }
});

then you can use the postMessage to send it with the:

let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
this.http.post('URL', postMessage, options)

1 Comment

you can make more assumptions from his previous question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.