gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:14:02 24 4
gpt4 key购买 nike

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

这是两个数组:

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);
}

最佳答案

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

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);
}

关于javascript - 在 AngularJS 中使用 $http 服务发布多个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32918909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com