Reputation: 98
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.
Upvotes: 2
Views: 71
Reputation: 286
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.
Upvotes: 1
Reputation: 32127
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);
});
Upvotes: 4