Ajouve
Ajouve

Reputation: 10089

Send json data to nodejs server

I have a problem sending json data to my node server

I tried

var req = {
        method: 'POST',
        url: 'http://33.33.33.15/user/signin',
        headers : {
      'Content-Type' : 'application/x-www-form-urlencoded'
    },
        data: {test:"test"}
 };

when I console.log() req.body y have

{ '{"test":"test"}': '' }

When I try with

var req = {
        method: 'POST',
        url: 'http://33.33.33.15/user/signin',
        headers : {
      'Content-Type' : 'application/x-www-form-urlencoded'
    },
        data: 'test=test'
 };

I have a good result on the server

I set the content type to application/x-www-form-urlencoded to allow the cross domain

On the server I have

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

Thanks for the help

Upvotes: 0

Views: 6753

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

If you want to send it as JSON, then you may consider doing so:

var req = {
    method: 'POST',
    url: 'http://33.33.33.15/user/signin',
    headers : {
        'Content-Type' : 'application/json'
    },
    data: JSON.stringify({ test: 'test' })
};

Upvotes: 2

Related Questions