0

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

1 Answer 1

2

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' })
};
Sign up to request clarification or add additional context in comments.

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.