Post JSON data from AngularJS client to Express Server

I am trying to post a JSON object using the AngularJS $ http service on Express Server. But I am getting an empty object on the server: "{}"

I see this thread but it doesn't solve my problem: angular post json to express

Here's the Angular client code:

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

      

I am checking the ticket object and is not empty

And here, express server:

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);

      

Thanks in advance for your help

+3


source to share


2 answers


Use data

property instead body

in call$http



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

      

+1


source


Replace

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

      



from

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

      

0


source







All Articles