[英]Where is data in angularjs http post request?
I'm not sure if I'm just being dumb but here goes. 我不确定我是否只是傻瓜,但事情就这样了。 I'm trying to send data to my server through a angularjs button which runs a function with HTTP post request. 我正在尝试通过angularjs按钮将数据发送到服务器,该按钮运行带有HTTP发布请求的功能。 However I'm not sure where the data is stored in the request. 但是我不确定数据在请求中的存储位置。 This is my button function: 这是我的按钮功能:
<script>
var app = angular.module('buttonApp', []);
app.controller('myCtrl', function($scope, $http) {
$scope.myFunc = function(url) {
console.log("func called");
$http({
method: 'POST',
url: url,
data: 'some data'
}).then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
console.log("Failed connection");
});
}
});
</script>
Here is my server code: 这是我的服务器代码:
app.post('/buttonPressed', function(req, res){
button = true;
console.log(req);
res.send("success");
});
If I console.log the 'req' I get a huge JSON structure where I can't find any reference to 'data' that I'm looking for. 如果我console.log'req',我将得到一个巨大的JSON结构,在该结构中找不到我要查找的对'data'的引用。 Am I on completely the wrong track? 我是在完全错误的轨道上吗?
Use middilware 使用中间件
app.use(express.bodyParser());
then 然后
app.post('/buttonPressed', function(req, res){
button = true;
req.body; //get the body parameter (POST data)
res.send("success");
});
It will give the post data sent in body 它将给出正文中发送的帖子数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.