Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post JSON data from AngularJS Client to Express Server

I try to post a JSON Object with AngularJS $http service to an Express Server. But I get an empty object in the server : "{}"

I see this topic but this not solve my issue : angular post json to express

Here code of Angular client :

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

I check the "ticket" object and it isn't empty

And here, the 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);

Thank you in advance for your help

like image 568
Varkal Avatar asked May 01 '26 11:05

Varkal


1 Answers

Use data property instead of body in your $http call

$http({
            url: baseUrl+"features/",
            method: "POST",
            data: ticket,
            headers: {'Content-Type': 'application/json'}})
like image 141
Artem Petrosian Avatar answered May 03 '26 23:05

Artem Petrosian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!