Finnjon
Finnjon

Reputation: 671

Sending data from form to POST request in Angular

I want to make a POST request using Angular. I am using ng-submit to submit the form to the controller where I access the factory to make the request using:

$http.post('/droplets', data)

I need the data from the form fields to put in the "data" variable above to send to my API but I can't work out how to do this when it is more than one field. How is this done?

Upvotes: 1

Views: 816

Answers (1)

Deenadhayalan Manoharan
Deenadhayalan Manoharan

Reputation: 5444

Try this...

$http({
        url: '/droplets',
        method: "POST",
        data: JSON.stringify({application:app, from:data1, to:data2}),
        headers: {'Content-Type': 'application/json'}
      }).success(function (data, status, headers, config) {
           // this callback will be called asynchronously
    // when the response is available
        }).error(function (data, status, headers, config) {
            // called asynchronously if an error occurs
    // or server returns response with an error status.
        });
};

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

Upvotes: 2

Related Questions