【发布时间】:2016-10-27 11:27:04
【问题描述】:
我实现了一个 Java API 后端,它作为 API 应用程序托管在 Azure 中。我通过 Active Directory 开启了身份验证。在我配置的 api 应用程序的 CORS 设置中,允许来自每个来源的调用只是将星号设置为 URL。
现在我想从本地 AngularJS 应用程序访问 API。我的角度代码如下所示:
angular.module('demo', [])
.controller('GetDataController', function($scope, $http) {
$scope.login = function(user){
console.log("user: " + user, user);
$http.defaults.useXDomain = true;
delete $http.defaults.headers.common['X-Requested-With'];
$http.get('http://<myapiapp>.azurewebsites.net/api/contacts').
then(function(response) {
$scope.greeting = response.data;
console.log("$scope.greeting: " + $scope.greeting, $scope.greeting);
});
}
});
但我仍然在控制台中收到以下错误:
XMLHttpRequest cannot load https://login.windows.net/
12141bed-36f0-4fc6-b70c-43483f616eb7/oauth2/autho…
%2Fapi%2Fcontacts%23&nonce=ea6b31321cda45d6a4e881fbd0062974_20161026094547.
Redirect from 'https://login.windows.net/12141bed-36f0-4fc6-b70c-43483f616eb7/
oauth2/autho…%2Fapi%2Fcontacts%23&
nonce=ea6b31321cda45d6a4e881fbd0062974_20161026094547' to
'https://login.microsoftonline.com/12141bed-36f0-4fc6-b70c-43483f616eb7/
oaut…%2Fapi%2Fcontacts%23&nonce=ea6b31321cda45d6a4e881fbd0062974_20161026094547'
has been blocked by CORS policy: No 'Access-Control-Allow-Origin'
header is present on the requested resource.
Origin 'null' is therefore not allowed access.
那么如何从 AngularJS 代码中获取访问权限?
这也与我的帖子 here 有关,我尝试通过 curl 调用获得访问权限。
【问题讨论】:
标签: javascript angularjs api azure active-directory