服务器端

时间:2017-03-04 05:29:29

标签: javascript node.js angular express

我在服务器的req.body中遇到了问题,因为我收到req.body为{}为空,这是服务中的代码



addEmployeeCollection(addArray: EmployeeSchema) {
  let url: string = Constants.DOMAIN + Constants.CREATE_EMPLOYEE_ROUTE;
  console.log('addArray at employee service', addArray)
  var body = addArray;
  return this._httpService.post(url, body).map(res => res.json()).catch(this._errorHandler);
}




this._httpService.post(url,body)将转到拦截器,返回http.post方法,这里是拦截器代码,



post(url: string, body: Object, options ? : RequestOptionsArgs): Observable < Response > {
  this.headers.append('Content-Type', 'application/json');
  let token = localStorage.getItem('realtoken');
  console.log('http token', token);
  if (typeof url === 'string') {
    if (!options) {
      options = {
        headers: this.headers
      };
    }
    options.headers.set('Authorization', ` ${token}`);
  }
  return super.post(url, body, options).catch(this.catchAuthError(this));
}
&#13;
&#13;
&#13;

现在此处标题处的请求有效负载包含信息

enter image description here

服务器端的响应是

{"__v":0,"_id":"58ba4969ed69821fd6f7e573"}// details of employee is not saved

这里是服务器端代码

&#13;
&#13;
var mongoose = require('mongoose');
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');

var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: false
}));

//schema-models
var employeeCollection = require('../../model/employee.model.js');


router.post('/new', function(req, res) {

  var employee_data = req.body;
  console.log('req.body=====> ', req.body)
  var addcontent = new employeeCollection(employee_data);
  console.log('addcontent--->', addcontent);
  addcontent.save(function(err, data) {
    if (err) {
      res.send(err);
    } else {
      console.log('data', data);
      res.json(data);
    }
  });
});

module.exports = router;
&#13;
&#13;
&#13;

在终端req.body=====> {},员工详细信息在请求中正确发送,但在服务器端获取为空请求者 作为这个概念的初学者,我无法弄清楚我哪里出错了,请帮帮我

0 个答案:

没有答案