Search code examples
javascriptnode.jsbody-parser

Body parser in node


I'm usind body parser

app.use(bodyParser.json())

and when I receive some data on this route

saveTest = function(req, res){
            
            console.log(req.body);
        }

With this ajax request

var json_test = JSON.stringify(test);
    $.ajax({
        url:'/guardar-test',
        method:'POST',
        data: json_test
    });

I expected that if I send and object , the data will be parsed to json automatically but is not this way I I have to use stringify function, its something wrong?


Solution

  • Try to set contentType option to application/json while using $.ajax.