【问题标题】:Google plus CORS error谷歌加 CORS 错误
【发布时间】:2015-04-03 02:57:48
【问题描述】:

我已经实现了使用 https://developers.google.com/+/web/signin/server-side-flow 的 AngularJS 应用程序

问题是我的 AngularJS 在一台服务器上,而 Rest Api 在另一台服务器上。当我尝试登录时,一切正常,直到我的回调函数调用 rest api。

这是错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://liveboard-dev.locastic.com/api/v1/login. This can be fixed by moving the resource to the same domain or enabling CORS.

服务器没有问题,因为我可以从代码的任何部分调用此方法,除非我在弹出窗口关闭后直接从谷歌发送身份验证响应。

我调用api的angularjs代码是:

        $http.post(applicationSettings.authenticationAPIUrl, authResult['code'], {headers: {'Content-Type': 'application/octet-stream; charset=utf-8'}}).success(function (response) {

        identityService.setIdentity(response);
        deferred.resolve(response);

    }).error(function (error) {

        deferred.reject(error);
    });

【问题讨论】:

  • 是的,但我不想构建代理服务器,因为我的服务器应用程序已经是某种代理
  • 我的意思是你在 Angular js 上启用了 CORS 吗?
  • @NarekMamikonyan 你是什么意思?
  • 我已经发布了喜欢的答案

标签: javascript angularjs api oauth cors


【解决方案1】:

在 Angular 端启用 CORS

var myApp = angular.module('myApp', [
    'myAppApiService']);

myApp.config(['$httpProvider', function($httpProvider) {
        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common['X-Requested-With'];
    }
]);

【讨论】:

  • CORS 必须由请求的接收者而不是发送者启用。
  • @CBroe 它也需要在 AngularJS 中启用
  • @Narek:这可能是 Angular 特有的东西,但不是 CORS 的主要部分。
  • 服务器上的 @CBroe 一切正常,因为我可以毫无问题地发送请求。唯一的问题是当我发送从弹出谷歌窗口收到的代码时
  • @CBroe 这就是为什么我说它也是 :)
猜你喜欢
  • 2018-07-16
  • 2019-11-28
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-27
  • 2016-03-12
相关资源
最近更新 更多