Javascript 将数据从Angular发送到Express
我不明白为什么我不能用Angular.js Express获取我发布的表单中的数据 角度部分:Javascript 将数据从Angular发送到Express,javascript,angularjs,express,Javascript,Angularjs,Express,我不明白为什么我不能用Angular.js Express获取我发布的表单中的数据 角度部分: $http.post(baseURL+"/search", data).success(function(data, status) { $scope.results = data; }); 明示部分: app.use(bodyParser.urlencoded({ extended: false })); app.post('/search', function(req,
$http.post(baseURL+"/search", data).success(function(data, status) {
$scope.results = data;
});
明示部分:
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/search', function(req, res){
console.log(req.query, req.body, req.params);
});
日志为{}{}{}。
我不知道我做错了什么
我还尝试:
$http({
method: "POST",
url : baseURL+"/search",
data : {name: 'tete'},
headers: {'Content-Type': 'application/json'}
}).success( function(data){
console.log(data);
});
它也不起作用。默认情况下,Angular以JSON的形式发送数据
$httpProvider.defaults.headers.post //Content-Type: application/json
您只包括urlencoded
body解析器中间件。您需要包括bodyParser.json()
似乎Angular
$http
服务将数据作为JSON发送,而您缺少相应的bodyParser
尝试用app.use(bodyParser.json())替换您的bodyParser代码>在Express中的邮寄路线之前
app.use(bodyParser.json());
app.post('/search', function(req, res){
console.log(req.body);
});