我的问题
使用$ post工作正常,但当我将其更新为$ resource时,
我收到 unsupported_grant_type 的错误。
我尝试过SO的许多解决方案,但无法解决此问题。
以下是我的代码。
控制器
app.factory('authService', ['$http', '$q', 'localStorageService', '$resource', '$rootScope',
function ($http, $q, localStorageService, $resource, $rootScope) {
var authServiceFactory = {};
var _authentication = {
isAuth: false,
userName: ""
};
var url = function (relativeUrl) {
return $rootScope.apiBaseUrl + '/api/' + relativeUrl;
};
var authResource = $resource(url('account/:id'), null, {
register: {
method: 'POST',
url: url('account/register')
},
login: {
method: 'POST',
url: $rootScope.apiBaseUrl + '/token'
}
},
{
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/x-www-form-urlencoded'
}
});
var _login = function (loginData) {
// var data = "grant_type=password&username=" + loginData.userName + "&password=" + loginData.password;
loginData.grant_type = "password";
var deferred = $q.defer();
authResource.login($.param(loginData)).$promise
.then(function (response) {
localStorageService.set('authorizationData', { token: response.access_token, userName: loginData.userName });
_authentication.isAuth = true;
_authentication.userName = loginData.userName;
deferred.resolve(response);
});
return deferred;
};
var _fillAuthData = function () {
var authData = localStorageService.get('authorizationData');
if (authData) {
_authentication.isAuth = true;
_authentication.userName = authData.userName;
}
}
authServiceFactory.saveRegistration = _saveRegistration;
authServiceFactory.login = _login;
authServiceFactory.logOut = _logOut;
authServiceFactory.fillAuthData = _fillAuthData;
authServiceFactory.authentication = _authentication;
return authServiceFactory;
}]);
服务
{{1}}
我从这么多尝试中发现了什么?
我发现,我的请求没有任何标题。