节点Js将后期数据作为json密钥获取

时间:2017-02-14 13:56:21

标签: angularjs json node.js body-parser

  

NodeJS文件server.js

    var express     = require('express');
    var app         = express();
    var bodyParser  = require('body-parser');
    var morgan      = require('morgan');
    var mongoose    = require('mongoose');
    var moment      = require('moment');
    var http        = require('http');
    var jwt         = require('jsonwebtoken'); 
    var config      = require('./config'); 
    var User        = require('./app/models/user'); 

    var port = process.env.PORT || 8080; 
    mongoose.connect(config.database);
    app.set('superSecret', config.secret); 
    app.use(bodyParser.urlencoded());
    app.use(bodyParser.json());

    app.post('/onboardAuthentication', function(req, res) {
      res.setHeader('Access-Control-Allow-Origin', '*');
      res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE');
      res.setHeader('contentType', 'application/x-www-form-urlencoded;charset=utf-8;');  
      res.json(req.body);
    });
var server = http.createServer(app);
server.listen(port);
console.log('Magic happens at http://localhost:' + port);
  

Angular API命中代码,前端API命中

var serviceRoot='http://localhost:8080/onboardAuthentication';

            var deferred=$q.defer();

            var req = {
                method: 'POST',
                url: serviceRoot,
                data: { key: 'value' },
                contentType: 'application/x-www-form-urlencoded;charset=utf-8;'
            };

            $http(req).then(goodResponse,badResponse);

            return deferred.promise;

    function goodResponse(response)
    {
        console.log("Good response");
        console.log(response);

    }
    function badResponse(response)
    {
        console.log("Bad response");
        console.log(response.data);
    }
  

它打印结果在此格式中,它将后期数据作为键,值为空,但我想使用body-parsar节点js以json格式访问后期数据。

{"key":"value"}: ""

1 个答案:

答案 0 :(得分:0)

如果要发送简单的文本/ ASCII数据,那么x-www-form-urlencoded将起作用, 默认情况下,angular会将application / json发送到服务器。

player.getChild('ControlBar').getChild('ProgressControl').currentWidth()