简体   繁体   English

在Angular中将数据从表单发送到POST请求

[英]Sending data from form to POST request in Angular

I want to make a POST request using Angular. 我想使用Angular发出POST请求。 I am using ng-submit to submit the form to the controller where I access the factory to make the request using: 我正在使用ng-submit将表单提交给控制器,在控制器中,我可以使用以下命令访问工厂提出请求:

$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. 我需要将表单字段中的数据放入上方的“ data”变量中以发送给我的API,但是当它涉及多个字段时,我无法解决该问题。 How is this done? 怎么做?

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 参考: https : //docs.angularjs.org/api/ng/service/ $ http

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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