【发布时间】:2016-08-04 14:42:02
【问题描述】:
我必须使用 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 数据结果为空。为什么?
【问题讨论】:
-
我的控制器配置是:
iftttApp.controller('indexController', ['$scope', '$routeParams', '$window', '$http', function ($scope, $routeParams, $window, $http, ) { ... }); -
“JSON 未放入 HTTP 消息中,POST 数据结果为空” — 您如何确定这一点?您是否使用浏览器中的开发人员工具来检查网络流量?您是否尝试使用问题中未包含的一些服务器端 Java 来阅读它?
-
$scope.nome 和 $scope.regione 已定义?
标签: javascript angularjs json http