gpt4 book ai didi

javascript - AngularJS HTTP POST 不发送数据

转载 作者:行者123 更新时间:2023-12-02 14:19:32 25 4
gpt4 key购买 nike

我必须使用 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);
});

};

我的 Controller 配置是:

iftttApp.controller('indexController', 
['$scope', '$routeParams', '$window', '$http', function ($scope, $routeParams, $window, $http, ) { ... });

POST 成功到达我的 servlet,返回成功,但是 JSON 未放入 HTTP 消息中,POST 数据结果为空。为什么?

最佳答案

尝试下面的代码。实际上您的发布不是正确的 key 对,您的发布请求中的值为 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);
});

};

关于javascript - AngularJS HTTP POST 不发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38770512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com