[英]AngularJS HTTP POST doesn't send data
我必須使用Angular JS通過HTTP POST發送一個簡單的JSON對象。
我有一個簡單的ng-clik鏈接函數:
$scope.requestGoogleAuth = function() {
var googleCredentials = {
email: 'user@domain.com',
password: 'a2s4d'
};
console.log(JSON.stringify(googleCredentials));
/*$http({
url: '/MyServlet',
method: "POST",
data: JSON.stringify(googleCredentials),
headers: {'Content-Type': 'application/json'}
})*/
$http.post("/MyServlet", JSON.stringify(googleCredentials)).then(function success(response) {
$('#loginGoogleModal').modal('hide');
$("#notificationsWrapper").notify(
"Logged with Google",
{
className: 'success',
position: 'bottom right'
}
);
$scope.googleLogged = true;
console.log($scope.googleLogged);
}, function error(response) {
$('#loginGoogleModal').modal('hide');
$("#notificationsWrapper").notify(
"Failed to login with Google",
{
className: 'error',
position: 'bottom right'
}
);
$scope.googleLogged = false;
console.log($scope.googleLogged);
});
};
我的控制器配置是:
iftttApp.controller('indexController',
['$scope', '$routeParams', '$window', '$http', function ($scope, $routeParams, $window, $http, ) { ... });
POST成功到達我的servlet,返回成功,但是JSON沒有放入HTTP消息中,POST數據結果為空。 為什么?
嘗試下面的代碼。 實際上您的發布不是正確的密鑰對,在發布請求中的值為json。
$scope.requestGoogleAuth = function() {
var googleCredentials = {
email: 'user@domain.com',
password: 'a2s4d'
};
console.log(JSON.stringify(googleCredentials));
/*$http({
url: '/MyServlet',
method: "POST",
data: JSON.stringify(googleCredentials),
headers: {'Content-Type': 'application/json'}
})*/
var postvalue = {
"nome": $scope.nome,
"regione": $scope.regione
}
$http.post("/MyServlet", angular.toJson(postvalue)).then(function success(response) {
$('#loginGoogleModal').modal('hide');
$("#notificationsWrapper").notify(
"Logged with Google",
{
className: 'success',
position: 'bottom right'
}
);
$scope.googleLogged = true;
console.log($scope.googleLogged);
}, function error(response) {
$('#loginGoogleModal').modal('hide');
$("#notificationsWrapper").notify(
"Failed to login with Google",
{
className: 'error',
position: 'bottom right'
}
);
$scope.googleLogged = false;
console.log($scope.googleLogged);
});
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.