2

I don't understand why I cannot get the data I POST form with Angular.js Express.

Angular Part :

$http.post(baseURL+"/search", data).success(function(data, status) {
        $scope.results = data;
    });

Express Part :

app.use(bodyParser.urlencoded({ extended: false })); 
app.post('/search', function(req, res){
    console.log(req.query, req.body, req.params);
});

The log is {} {} {}. I can't figure out what am I doing wrong.

I also tried :

$http({
    method: "POST",
    url : baseURL+"/search",
    data : {name: 'tete'},
    headers: {'Content-Type': 'application/json'}
}).success( function(data){
    console.log(data);
});

It doesn't work too.

0

2 Answers 2

4

Angular sends data as JSON by default.

$httpProvider.defaults.headers.post //Content-Type: application/json

You're only including the urlencoded body-parser middleware. You need to include bodyParser.json().

app.use(bodyParser.json()); 
app.post('/search', function(req, res){
    console.log(req.body);
});
Sign up to request clarification or add additional context in comments.

Comments

1

It seems that Angular $http service sends data as JSON and you're missing appropriate bodyParser.

Try replacing your bodyParser with app.use(bodyParser.json()); before your POST route in Express.

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.