[英]Node / Express / Angular Server side external API request
我正在使用Angular将表单输入保存到$ scope.tag。 我无法使用表单信息作为参数进行客户端外部API调用,因此我需要将其传递给服务器。 我怎样才能做到这一点?
步骤:
我该如何实现?
$scope.tag = '';
// client side
$http.get('/api')
.then(function(response) {
console.log(response);
});
// server side
app.get('/api', function (req, res) {
request('http://externalAPI.com/' + $scope.tag, function (req, res) {
res.json(data);
});
});
您可以执行以下操作:
// client side
$http.post('/api', { tag: $scope.tag })
.then(function(response) {
console.log(response);
});
// server side
app.post('/api', function (req, res) {
console.log(req.query.tag);
res.json({ status: 'success' });
});
只记得包括app.use(bodyParser.json());
在路由中间件之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.