简体   繁体   中英

angular2 http.post request the req.body is empty

I am having issues with my angular 2 application , i am trying to send json oberts to my mongofb database , when i click the post it sends the _id to my mongodb however my req.body is empty

 import { Injectable } from '@angular/core'; import { HttpModule ,Http , Headers } from '@angular/http'; import 'rxjs/add/operator/map'; @Injectable() export class BookService { constructor(private _http:Http){ } getTodos(){ return this._http.get('/api/v1/book') .map(res => res.json()); } saveBooking(booking){ var headers = new Headers(); headers.append('Content-Type', 'application/json'); return this._http.post('/api/v1/book' , { b: 'texts.value', isCompleted: 'b'}, {headers: headers}) .map(res => res.json()); } } 

And this is my router.js

 var express = require('express'); var router = express.Router(); var mongojs = require('mongojs'); var db = mongojs('mongodb://lee:Zzbawsoldd1@ds163630.mlab.com:63630/bookings_drbookings',['bookings']); router.get('/book', function(req,res,next){ db.bookings.find(function(err, bookings){ if(err){ res.send(err); }else{ res.json(bookings); } }); }); // Get Single Todo router.get('/book/:id', function(req, res, next){ db.bookings.findOne({ _id: mongojs.ObjectId(req.params.id) }, function(err, todo){ if(err){ res.send(err); } else { res.json(todo); } }); }); router.post('/book', function(req, res, next){ /*if(!todo.text || !(todo.isCompleted + '')){ res.status(400); res.json({ "error": "Invalid Datazz" }); } else {*/ db.bookings.save(req.body, function(err, result){ if(err){ res.send(err); } else { res.json(result); } }); }); router.put('/book/:id', function(req, res, next){ var bookinfo = req.body; var updObj = {}; if(bookinfo.isCompleted){ updObj.isCompleted = bookinfo.isCompleted; } if(bookinfo.text){ updObj.text = bookinfo.text; } if(!updObj){ res.status(400); res.json({ "error": "Invalid Data" }); } else { db.bookings.update({ _id: mongojs.ObjectId(req.params.id) },updObj, {}, function(err, result){ if(err){ res.send(err); } else { res.json(result); } }); } }); router.delete('/book/:id', function(req, res, next){ db.bookings.remove({ _id: mongojs.ObjectId(req.params.id) },'', function(err, result){ if(err){ res.send(err); } else { res.json(result); } }); }); module.exports = router; 

My request to server is working however it is not sending the json object from my service to the router.js

Try below code snap:

  saveBooking(booking){
    var headers = new Headers();
    headers.append('Content-Type', 'application/json');
    let data:any={};
    data.b='texts.value';
    data.isCompleted='b';
    return this.http.post('/api/v1/book' , data, {headers: headers}) .map(res =>  res.json());
}

Thank you for the help guy but I found the soloution

There was a mistake in my server.js file I wrote app.set instead of app.use in my express server code

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