javascript - 在 Angular 中发布 req.body.variable_name

标签 javascript angularjs node.js express

我想发布 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) $scopeapiSvc

不要使用成功,使用然后

工厂

在工厂中不要使用成功将工厂更改为

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/

相关文章:

javascript - 在 FlatList React-native 中获取 Promise 作为返回值

javascript - interact.js 拖放引用 div

angularjs - 单击表中的列 - Protractor

javascript - AngularJS 服务对象未在 View / Controller 之间更新

node.js - 子进程未调用关闭事件

javascript - <a> 标签上的下载属性在 Chrome 中不起作用

javascript - 无法引用从标题中包含空格的 csv 文件导入的 d3.js 数据

javascript - 为什么我的页面在 modal.close() 之后重新加载?

javascript - 如何在 webpack 开发服务器启动后自动运行 NPM 脚本?

node.js - PhpStorm/WebStorm node_modules 和 package.json