将AngularJS Client中的JSON数据发布到Express Server

时间:2015-04-30 08:39:40

标签: javascript json angularjs post express

我尝试将带有AngularJS $ http服务的JSON对象发布到Express Server。但是我在服务器中得到一个空对象:“{}”

我看到了这个主题,但这不能解决我的问题:angular post json to express

这里是Angular客户端的代码:

self.postTicket = function(ticket){
        $http({
            url: baseUrl+"features/",
            method: "POST",
            body: ticket,
            headers: {'Content-Type': 'application/json'}})
}

我检查“票证”对象并且它不是空的

在这里,快递服务器:

var express = require("express");
var request = require("request");
var bodyParser = require('body-parser')


var app = express();

app.use(bodyParser.json({}));

app.post('*', function (req, res) {
    console.log(req.body);
    res.status(200).send('OK');
});


app.listen(9000);

提前感谢您的帮助

2 个答案:

答案 0 :(得分:1)

data电话

中使用body媒体资源代替$http
$http({
            url: baseUrl+"features/",
            method: "POST",
            data: ticket,
            headers: {'Content-Type': 'application/json'}})

答案 1 :(得分:0)

替换

self.postTicket = function(ticket){
    $http({
        url: baseUrl+"features/",
        method: "POST",
        body: ticket,
        headers: {'Content-Type': 'application/json'}})

self.postTicket = function(ticket){
    $http({
        url: baseUrl+"features/",
        method: "POST",
        data: ticket,
        headers: {'Content-Type': 'application/json'}})