Angularjs express中未定义Post参数
我正在学习Web开发的MEAN stack。我在客户端有以下代码:Angularjs express中未定义Post参数,angularjs,node.js,express,httprequest,httpresponse,Angularjs,Node.js,Express,Httprequest,Httpresponse,我正在学习Web开发的MEAN stack。我在客户端有以下代码: var req = { method: 'POST', url: 'http://localhost:3000/Sad/3', headers: { 'Content-Type': "Text" }, data: {ID: Auth.sadit , UID: $scope.Sodid , PID: 1234} }; 服务器端有以下内容: app.post('/Sad/:Type', function(req,
var req = {
method: 'POST',
url: 'http://localhost:3000/Sad/3',
headers: {
'Content-Type': "Text"
},
data: {ID: Auth.sadit , UID: $scope.Sodid , PID: 1234}
};
服务器端有以下内容:
app.post('/Sad/:Type', function(req, res) {
var param = req.data;
var qry = req.headers;
var url =req.url;
console.log(req.data + "\n");
if(req.params.Type == 1){
<print all the parameters in request>
}
if(req.params.Type == 3){
<Print the parameters in the request , change them and send it back>
}
res.status(200).send( " Hello User");
});
app.post('/Sad/:Type',函数(req,res){
var param=请求数据;
var qry=请求标头;
var url=req.url;
控制台日志(请求数据+“\n”);
如果(req.params.Type==1){
}
如果(req.params.Type==3){
}
res.status(200)。发送(“你好用户”);
});
现在,当我尝试打印从client express发送的数据中的参数时,只会给出未定义的参数。看起来您正在向api发送json,因此
内容类型应为应用程序/json
而不是文本,我还将使用$http或$resourceyup,我自己使用$http。以下代码在cleint端,我使用的是http服务app.controller('cont',['scope','AU','http',函数($scope,AU,$http){var req={method:'POST',url:'',头:{'Content Type':“application/json”},数据:{ID:Auth.sadit,UID:$scope.Sodid,PID:1234};$http然后(function(){Auth.satid+=1;console.log(“发送请求”+req.data);},function(){console.log(“失败”);});在wireshark中,我看到http数据包作为jason对象携带这些值,但我无法在express中解析它们