Javascript 节点/快速/角度服务器端外部API请求
我正在使用Angular将表单输入保存到$scope.tag。我无法使用表单信息作为参数进行客户端外部API调用,因此需要将其传递给服务器。我怎样才能做到这一点Javascript 节点/快速/角度服务器端外部API请求,javascript,angularjs,node.js,api,Javascript,Angularjs,Node.js,Api,我正在使用Angular将表单输入保存到$scope.tag。我无法使用表单信息作为参数进行客户端外部API调用,因此需要将其传递给服务器。我怎样才能做到这一点 $scope.tag = ''; // client side $http.get('/api') .then(function(response) { console.log(response); }); // server side app.get('/api', function (
$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);
});
});
步骤如下:
$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())代码>在路由中间件之前。您可以执行以下操作:
// 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())代码>在路由中间件之前。您可以执行以下操作:
// 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())代码>在路由中间件之前。您可以执行以下操作:
// 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())
在您的路由中间件之前。和npm安装body parser
并在服务器js文件中要求它,因为它现在单独出现:PThis不起作用。使用post给了我以下错误:加载资源失败:net::ERR_CONNECTION\u refused客户端的post似乎没有生成,并给了我404(未找到)和npm install body parser
,要求在服务器js文件中加载,因为它现在单独出现:p此操作不起作用。使用post给了我以下错误:加载资源失败:net::ERR_CONNECTION\u refused客户端的post似乎没有生成,并给了我404(未找到)和npm install body parser
,要求在服务器js文件中加载,因为它现在单独出现:p此操作不起作用。使用post给了我以下错误:加载资源失败:net::ERR_CONNECTION\u refused客户端的post似乎没有生成,并给了我404(未找到)和npm install body parser
,要求在服务器js文件中加载,因为它现在单独出现:p此操作不起作用。使用post给了我以下错误:加载资源失败:net::ERR_CONNECTION_refured客户端的post似乎没有生成,并给了我404(未找到)