谷歌加 CORS 错误

Google plus CORS error

本文关键字:错误 CORS 谷歌      更新时间:2023-09-26

我已经实现了使用 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.

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

我用于调用 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);
    });

在角度侧启用 CORS

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