在AngularJS中使用$ http服务发布多个数组

时间:2015-10-03 04:28:41

标签: javascript angularjs

我正在使用并且有两个数组,我想发布到服务器进行处理并在确认电子邮件中发送。有关如何正确提交这些数组的任何想法?

以下是两个数组:

var array1 = vm.contacts;
var array2 = vm.projects;

$ http服务:

data = array1; // Is it possible to add array2 here too?

$http.post('http://localhost:9000/api/emails', data)
  .then(function(response) {
      console.log(response);

    }, function(response) {
      console.log('error', response);
    }

1 个答案:

答案 0 :(得分:3)

您可以发送包含这些数组的对象。像这样:

var vm = {};
vm.contacts = []; // Array of contacts.
vm.projects = []; // Array of projects.
var data = vm; // Object with arrays.

在$ http服务中

$http.post('http://localhost:9000/api/emails', data)
    .then(function (response) {
    console.log(response.data); // Use response.data to show your response.
}, function (response) {
    console.log('error', response);
}

<强>更新

通过这种方式,您可以发送数组数组。像这样:

var vm = {};
vm.contacts = [];
vm.projects = [];

var arrays = [];
var array1 = vm.contacts;
var array2 = vm.projects;

arrays.push(array1, array2);
console.log(arrays);
var data = arrays;

然后:

在$ http服务中

$http.post('http://localhost:9000/api/emails', data)
    .then(function (response) {
    console.log(response.data); // Use response.data to show your response.
}, function (response) {
    console.log('error', response);
}