gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:44:31 24 4
gpt4 key购买 nike

我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com