简体   繁体   中英

Angular - $http.post send data as json

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 ?

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)

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

      $http.post('/saveusers', users).then(function(data) {
        $scope.message = data;
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM