Javascript AngularJS HTTP POST不';不要发送数据
我必须通过HTTP POST使用Angular JS发送一个简单的JSON对象 我有一个简单的ng clik链接函数:Javascript AngularJS HTTP POST不';不要发送数据,javascript,angularjs,json,http,Javascript,Angularjs,Json,Http,我必须通过HTTP POST使用Angular JS发送一个简单的JSON对象 我有一个简单的ng clik链接函数: $scope.requestGoogleAuth = function() { var googleCredentials = { email: 'user@domain.com', password: 'a2s4d' }; console.log(JSON.stringify(go
$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);
});
};
试试下面的代码。实际上,您的发布不是一个正确的密钥对,而是在您的发布请求中使用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);
});
};
我的控制器配置是:
ifttapp.controller('indexController',['$scope','$routeParams','$window','$http',function($scope,$routeParams,$window,$http,){…});
“JSON没有放在http消息中,POST数据结果为空”-您如何确定这一点?您是否使用浏览器中的开发人员工具检查网络流量?您是否尝试使用问题中未包含的服务器端Java阅读它?$scope.nome和$scope.regione已定义?我的控制器配置为:ifttapp.controller('indexController'),['$scope','$routeParams','$window','$http',function($scope,$routeParams,$window,$http,){…});
“JSON没有放在http消息中,POST数据结果为空”-您是如何确定这一点的?您是否使用浏览器中的开发人员工具来检查网络流量?您是否尝试使用问题中未包含的服务器端Java来阅读它?$scope.nome和$scope.regione已定义?似乎您发布的是字符串,而不是json对象这就是为什么我使用ngular.toJson()。它对我有用。我更正了代码,并测试了“angular.toJson(googleCredentials)”,但不工作。请检查浏览器中的网络工具发布正文信息。以及您在服务器端如何接收json值?似乎您发布的是字符串,而不是json对象。这就是为什么我使用angular.toJson()将其转换为json。它对我有效。我更正了代码,并测试了“angular.toJson”(googleCredentials)”,但不工作。请检查浏览器中的网络工具帖子正文信息。以及您在服务器端如何接收json值?