gpt4 book ai didi

javascript - 如何通过 AngularJS $http 从 ASP.Net Web API 2 获取访问 token ?

转载 作者:行者123 更新时间:2023-12-03 21:45:24 27 4
gpt4 key购买 nike

我尝试这样:

$http({ method: 'POST', url: '/token', data: { username: $scope.username, password: $scope.password, grant_type: 'password' } }).success(function (data, status, headers, config) {
$scope.output = data;
}).error(function (data, status, headers, config) {
$scope.output = data;
});

然后尝试将 grant_type 更改为参数:

$http({ method: 'POST', url: '/token', data: { username: $scope.username, password: $scope.password }, params: { grant_type: 'password' } }).success(function (data, status, headers, config) {
$scope.output = data;
}).error(function (data, status, headers, config) {
$scope.output = data;
});

仍然会遇到可怕的情况:{"error":"unsupported_grant_type"}

所以我做了 AngularJS 开发人员不应该做的事情,诉诸 jQuery:

var data = $('#regForm').serialize() + "&grant_type=password";
$.post('/token', data).always(showResponse);

function showResponse(object) {
$scope.output = JSON.stringify(object, null, 4);
$scope.$apply();
};

这就像冠军一样......所以我的问题是:我们如何使用 AngularJS $http() 复制上面的 jQuery $.post() 调用,所以我们可以从 ASP.Net Web API 2 中基于 OWIN 中间件的/token 端点获取访问 token 吗?

最佳答案

这样做:

$http({
url: '/token',
method: 'POST',
data: "userName=" + $scope.username + "&password=" + $scope.password +
"&grant_type=password"
})

关于javascript - 如何通过 AngularJS $http 从 ASP.Net Web API 2 获取访问 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20376738/

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