我尝试这样:
$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();
};
哪个像冠军一样工作......所以我的问题是:我们如何$.post()
使用 AngularJS 复制上面的 jQuery 调用,$http()
以便我们可以从 ASP.Net Web API 2 中基于 OWIN 中间件的 /token 端点获取访问令牌?