我想发布 req.body.variable_name ,请参阅下面的我的 Controller 和服务。还有nodejs端。我在responseAddROBO 中收到 undefined ,错误在哪里?
var ROBO = { host: $scope.hostname,
subnet: $scope.subnet };
$scope.addrobo = function () {
apiSvc.post('/api/addrobo/', ROBO).success(function (response) {
$scope.responseAddROBO = response;
})
.error(function (response) {
$scope.responseAddROBO = "NO RESPONSE";
}
);
$scope.hostname = '';
$scope.subnet = '';
}
服务
.factory("apiSvc", function($http) {
return {
post: function(url,data) {
return $http.post(url,data).success(function(response) {
return response
});
}
}
});
nodejs 端的路由:
router.post('/api/addrobo/', function (req, res, next) {
res.send(req.body.host,req.body.subnet);
});
最佳答案
Controller
仔细检查您是否已注入(inject) $scope
和 apiSvc
不要使用成功
,使用然后
工厂
在工厂中不要使用成功
将工厂更改为
return $http.post(url,data);
因为当前您在一条数据上获得了两次成功,而实际上它应该在 Controller 中完成一次。
你有一个数组
当你获取[object Object]
时,你有一个数组而不仅仅是一个JSON对象。您可以使用 JSON.stringify
方法从现有对象生成 JSON 字符串:
alert(JSON.stringify(changed));
源代码:Why am I getting [object Object] instead of JSON?
给作者点赞
关于javascript - 在 Angular 中发布 req.body.variable_name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32703218/