我有一个 id 数组,我想将其传递给服务器。我在网上看到的其他答案描述了如何将这些 id 作为 url 中的查询参数传递。我不喜欢使用这种方法,因为可能有很多 id。这是我尝试过的:
AngularJS:
console.log('my ids = ' + JSON.stringify(ids)); // ["482944","335392","482593",...]
var data = $.param({
ids: ids
});
return $http({
url: 'controller/method',
method: "GET",
data: data,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function (result, status, headers, config) {
return result;
})
Node.js:
app.get('/controller/method', function(req, res) {
console.log('my ids = ' + JSON.stringify(req.body.ids)); // undefined
model.find({
'id_field': { $in: req.body.ids }
}, function(err, data){
console.log('ids from query = ' + JSON.stringify(data)); // undefined
return res.json(data);
});
});
为什么我在服务器端收到undefined
?我怀疑这是因为我使用了 $.params
,但我不确定。
最佳答案
如果您想在 AJAX 调用中使用属性 data
来发送需要更改的更多信息,则在 Rest GET
方法中,使用 URL 作为传输信息的方法该方法为 POST
方法。
因此,在服务器中,您将声明更改为:
app.post(
而不是 app.get(
关于node.js - 使用 $http 时如何将数组传递到服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25587729/