javascript - 从 Angular 到 Node 的 POST 数据不起作用

标签 javascript angularjs node.js forms

我正在使用 Angular 将数据发送到 Node 服务器,但它不起作用。我无法在我的 Node 发布方法中看到我的发布数据(表单中的输入值)。我需要将数据保存在 mongoDb 中,但我不知道为什么看不到前端发送的 POST 值。

我无法通过 console.log() 在 Node 中查看我的表单数据。有时它会返回 {},有时当我使用

时,“morgan”调试器会将 POST 识别为 OPTIONS req
console.log(req.body.fname);

我的 HTML:

    <body ng-app="app" ng-controller="MyController">
    <div id="login-page" class="row">
        <div class="col s12 z-depth-6 card-panel">
            <form ng-submit="postData();">
                <div class="row" style="height: 90px;background-color:#00BFA5;margin: -12px -12px 15px -12px">
                    <h3 class="center-align">Register</h3>
                </div>
                <div class="row margin">
                    <div class="input-field col s12">
                        <i class="mdi-social-person prefix"></i>
                        <input id="fname" name="fname" ng-model="formData.fname" type="text" class="validate">
                        <label for="fname" class="center-align">First Name</label>
                    </div>
                </div>

                <div class="row margin">
                    <div class="input-field col s12">
                        <i class="mdi-social-person-outline prefix"></i>
                        <input id="lname" name="lname" ng-model="formData.lname" type="text" class="validate">
                        <label for="lname" class="center-align">Last Name</label>
                    </div>
                </div>
                <div class="row margin">
                    <div class="input-field col s12">
                        <i class="mdi-action-account-circle prefix"></i>
                        <input id="username" type="text" name="formData.username" ng-model="username" class="validate">
                        <label for="username" class="center-align">Username</label>
                    </div>
                </div>
                <div class="row margin">
                    <div class="input-field col s12">
                        <i class="mdi-communication-email prefix"></i>
                        <input id="email" type="email" class="validate" name="email" ng-model="formData.email">
                        <label for="email" class="center-align">Email</label>
                    </div>
                </div>
                <div class="row margin">
                    <div class="input-field col s12">
                        <i class="mdi-action-lock prefix"></i>
                        <input id="password" type="password" class="validate" name="password" ng-model="formData.password">
                        <label for="password">Password</label>
                    </div>
                </div>
                <div class="row margin">
                    <div class="input-field col s12">
                        <i class="mdi-action-lock-outline prefix"></i>
                        <input id="password-again" type="password" name="pass_again">
                        <label for="password-again">Re-type password</label>
                    </div>
                </div>

                <div class="row">
                    <button class="btn waves-effect waves-light validate col s6 push-s3"
                            style="margin-top: 15px;"
                            type="submit">Submit
                        <i class="material-icons right">send</i>
                    </button>
                    <div class="input-field col s12">
                        <p class="margin center medium-small sign-up">Already have an account? <a
                                href="login.html">Login</a></p>
                    </div>
                </div>
            </form>
            {{ServerResponse}}
        </div>
    </div>
    <script type="text/javascript" src="js/angular_min.js"></script>
    <script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
    <script src="js/materialize.min.js"></script>
    <script>
        angular.module('app', []).controller('MyController', function ($scope, $http) {
            $scope.postData = function () {
                $http.post('http://localhost:3000/api/post', $scope.formData)
                        .success(function (data) {
                            $scope.ServerResponse = data;
                        }).error(function (data) {
                    $scope.ServerResponse = data;
                })
            }
        })
    </script>
    </body>

我的 Node 代码:

    require('rootpath')();
    var express = require('express');
    var app = express();
    var bodyParser = require('body-parser');
    var morgan = require('morgan');
    var mongoose = require('mongoose');

    var jwt = require('jsonwebtoken');
    var config = require('config.json');
    app.set('view engine', 'ejs');
    app.set('views', __dirname + '/views');

    var port = process.env.PORT || 3000;
    mongoose.connect(config.connectionString);
    app.set('superSecret', config.secret);
    app.use(bodyParser.urlencoded({extended: true}));
    app.use(bodyParser.json());
    app.use(morgan('dev'));
    app.post('/api/post', function (req, res) {
        console.log(req.body.fname);
    });
    app.listen(port);
    console.log('Magic happens at http://localhost:' + port);

谢谢。

最佳答案

请添加到您的模块

$httpProvider.defaults.headers.post['Content-Type'] = 
'application/x-www-form-urlencoded;charset=utf-8';

关于javascript - 从 Angular 到 Node 的 POST 数据不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35294839/

相关文章:

javascript - 对 CSS 样式表的更改不会反射(reflect)在 Angular.js 按钮中。 JS 重写 CSS 了吗?

node.js - 更新到 Grunt 导致 "Use of const in strict mode"错误

javascript - AmCharts.js - 将默认值 "compareto"设置为全部而不是无

javascript - 从 firebase 数组中选择 "column"

javascript - 为什么在 fetch promise 和 this.setState 之后打印控制台日志,当我单击按钮时

php - 无法在 MySQL、PHP、AngularJS 中插入多行

javascript - 从 Node.js 模块访问全局变量?

javascript - 多个主题和订阅或只有一个 - Angular 5

javascript - 自动滚动页面+关闭侧边栏菜单项选择后+链接菜单项与部分ID(Wordpress - Jquery)

javascript - 了解表单中使用的语言