简体   繁体   中英

Angular2 http.post and undefined req.body

I have a problem with receiving json from http.post. Let's make it clear a bit: This is my component file:

import {Component} from 'angular2/core'
import {Http, HTTP_PROVIDERS, Headers} from 'angular2/http'

@Component({
  selector: 'login-form',
  template: `
    <form (ngSubmit)="onSubmit()">

      <button type="submit">Wyslij</button>
    </form>
  `
})

export class LoginFormComponent {
  constructor(public http: Http) { }

  onSubmit() {
    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');

    var data = JSON.stringify({
      login: "zupa",
    });

    this.http.post('http://localhost:8080/send', data, {headers: headers})
    .subscribe();
  }
}

My server.js file

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'node_modules')));
app.use(express.static(path.join(__dirname)));
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);

app.get('*', function(req, res, next) {
  res.render("index.html", {title: 'Aplikacja'});
});

app.listen(8080, function() {
  console.log("Starting at localhost:8080");
});

module.exports = app;

As you can see, this is a standard generated by express module server file.

This is content of my "routes" file.

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index.html', {title: 'Infam'});
});

router.post('/send', function(req, res, next) {
  console.log("Received data: " + req.body.login);
});

module.exports = router;

The server returns a message to me: "Received data: undefined " and I have no idea why the req.body.login is undefined. Can you help me? Thank you in advance.

PS: The same is when I use

"login": "zupa"

instead of

login: "zupa"

您应该设置适当的标头,因为您使用的是JSON。

headers.append('Content-Type', 'application/json');

Check the accepted answer here Angular2 - set headers for every request

It includes a custom http client to process your requests so you don't have to write the headers for every single request you make.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM