我正在使用Angular JS向http帖子发送一个json对象。
var obj = {
"username": email,
"group": "team | " + designation
};
$http.post(`${API_URL}/member`, obj)
.then((data) => {
$mdToast.show(
$mdToast.simple()
.textContent('created successfully')
.hideDelay(3000)
)
$route.reload()
}, (error) => {
})
从服务器端,我将该用户名和组数据发送到data.attributes,如下所示。
request.body.data.attributes.username
我也试过其他方法。但它是一样的。我需要将这些数据提供给request.body。
request.body.username
答案 0 :(得分:0)
请提及我尝试过以上的完整示例及其正常工作。
(function () {'use strict';
angular.
module('myApp', ['ngMaterial', 'ngRoute']).
controller('HomeCtrl', HomeCtrl);
function HomeCtrl($mdToast, $scope, $http, $route) {
$scope.showToast = function(content) {
var obj = {
"username": "as@aa.aa",
"group": "team | " + "IT"
};
$http.post("https://httpbin.org/post", obj)
.then((data) => {
console.log(data.data.json);
$mdToast.show(
$mdToast.simple()
.textContent('created successfully')
.hideDelay(3000)
)
$route.reload()
}, (error) => {
})
}
}
}());

<html ng-app="myApp">
<head>
<link href="https://rawgit.com/angular/bower-material/master/angular-material.css" rel="stylesheet"/>
</head>
<body>
<div ng-controller="HomeCtrl" class="md-padding" id="popupContainer" ng-cloak>
<md-content layout="column" layout-fill="" layout-padding="">
<md-button class="md-primary md-raised" ng-click="showToast('Test')">
Show toast
</md-button>
</md-content>
</div>
<script src="https://code.angularjs.org/1.3.13/angular.min.js"></script>
<script src="https://code.angularjs.org/1.3.13/angular-animate.min.js"></script>
<script src="https://code.angularjs.org/1.3.13/angular-aria.min.js"></script>
<script src="https://rawgit.com/angular/bower-material/master/angular-material.min.js"></script>
<script src="https://code.angularjs.org/1.3.13/angular-route.min.js"></script>
</body>
</html>
&#13;