javascript - 在 angularjs 中使用 $http.post() 时出现 CORS 问题

标签 javascript angularjs cors openid-connect identityserver3

我正在创建一个用于 IdentityServer 3 的测试应用程序,并使用 AngularJs、Angular Ui Router 和 oauth-ng 库来创建它。我的应用程序需要在客户端使用 OpenID 连接授权代码流。所以我对 oauth-ng 库做了很小的更改。我使用 oauth-ng lib 从 IdentityServer3 检索授权代码,并使用 $http.post() 将该代码发送到 token 端点并获取 access_token。

以下是我的代码..

JavaScript

.controller('LoginController', function ($scope, $http, $timeout, $location) {

        var getParamsFromUrl = function(url) {
            var splitted = url.split('?');
            splitted = splitted[1].split('&');
            var params = {};

            for (var i = 0; i < splitted.length; i++) {
                var param  = splitted[i].split('=');
                var key    = param[0];
                var value  = param[1];
                params[key] = value
            }
            return params;
        };

        var getToken = function (url, data) {
            return $http.post(url, data);
        };



        if($location.absUrl().split('?')[1]) {
            $scope.params = getParamsFromUrl($location.absUrl());

            var tokenData = {};
            tokenData.grant_type = 'authorization_code';
            tokenData.code = $scope.params.code;
            tokenData.redirect_uri = 'http://localhost:8000/login.html';

            var tokenEndpoint = 'https://localhost:44333/core/connect/token';

            getToken(tokenEndpoint, tokenData)
                .then(function (data, status, headers, config) {
                    console.log(data);
                    console.log(status);
                    console.log(headers);
                    console.log(config);
                });

        }
    });

授权代码已成功检索,当我尝试连接到 token 端点时,它会在浏览器上抛出以下错误。

OPTIONS https://localhost:44333/core/connect/token b @ angular.min.js:87n @ angular.min.js:82$get.f @ angular.min.js:80(anonymous function) @ angular.min.js:112$get.n.$eval @ angular.min.js:126$get.n.$digest @ angular.min.js:123$get.n.$apply @ angular.min.js:126(anonymous function) @ angular.min.js:17e @ angular.min.js:36d @ angular.min.js:17uc @ angular.min.js:18Jd @ angular.min.js:17(anonymous function) @ angular.min.js:250n.Callbacks.j @ jquery.min.js:2n.Callbacks.k.fireWith @ jquery.min.js:2n.extend.ready @ jquery.min.js:2I @ jquery.min.js:2

XMLHttpRequest cannot load https://localhost:44333/core/connect/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. The response had HTTP status code 405.

然后我尝试使用这行代码在应用程序配置中配置 $httpProvider 。我的配置如下所示。

.config(function ($locationProvider, $httpProvider) {
        $locationProvider.html5Mode(true);
        $httpProvider.defaults.withCredentials = true; // <= Added this
    });

仍然遇到同样的问题。我如何从客户端解决这个问题?我可以从客户端解决这个问题吗?

最佳答案

您请求的服务不允许 CORS(响应中不会发送访问控制)。您需要包含 header Access-Control-Allow-Origin: * 作为响应的一部分。

看这个:http://www.html5rocks.com/en/tutorials/cors/

另请参阅:http://better-inter.net/enabling-cors-in-angular-js/

关于javascript - 在 angularjs 中使用 $http.post() 时出现 CORS 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29838433/

相关文章:

javascript - React JS Nuka轮播自定义箭头定位

javascript - openweathermap API 到 Angular ES6

canvas - 我应该将 CORS 设置设置为什么以及在哪里设置,这样我的 Canvas 就不会被污染?

scala - Scala Play 2.2 应用程序中的 CORS 过滤器

azure - Azure CDN 上的 ttf 字体文件出现 CORS 错误

javascript - 如何在滚动时从使用 javascript 加载元素的网页中进行抓取?

javascript - 如何使用 Nodemailer 从表单发送电子邮件

javascript - 如何在 SweetAlert2 html 输出中使用 v-for

javascript - 当 child 被过滤掉时,如何让 parent 不显示?

AngularJS:返回 promise 拒绝