3

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

2 Answers 2

1

Use data property instead of body in your $http call

$http({
            url: baseUrl+"features/",
            method: "POST",
            data: ticket,
            headers: {'Content-Type': 'application/json'}})
Sign up to request clarification or add additional context in comments.

Comments

0

Replace

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

with

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.