1

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?

2
  • 1
    why do you not just give the form action="/droplets" method="post" and let it submit itself? ng-submit will still be triggered Commented Jul 8, 2015 at 11:29
  • I don't want a page refresh. I want to use AJAX. Commented Jul 8, 2015 at 12:06

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. Can you explain: data: JSON.stringify({application:app, from:data1, to:data2})? I'm not sure what should go in the parentheses.
request like "{"application":"appname","from":"test","to":"test"}"
I'm still not clear. What do the "from:" and "to:" refer to?
from and to are key of test and test,see above json

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.