angularjs - 请求被重定向到 'https://..com/site/login?' ,这对于需要预检的跨源请求是不允许的

标签 angularjs google-chrome http cors

在发布这个问题之前,我已经完成了大部分堆栈溢出问题,但我没有得到答案

我正在尝试访问一个将执行 Oauth 身份验证的客户端 api,因此当我调用该 api 时,它应该抛出一个弹出窗口以进行类似于 fb auth 的身份验证,但我最终在我的 chrome 控制台中出现错误,如下所示

XMLHttpRequest cannot load https://api.springrole.com/authorize?
scope=XXXX&redirect_uri=http://localhost:8080/CheckRest/springrole.html&response_type=XXXX&client_id=XXXXXXXX. The request was redirected to 'https://springrole.com/site/login?api_login=1&scope=XXXX&state=&error=invalid_request', which is disallowed for cross-origin requests that require preflight.

为了安全起见,我刚刚用 XXX 屏蔽了一些值。我的请求如下所示

 $http({
        method : 'GET',

        url : url,
        headers : {
            'Content-Type' : 'application/json',
                "Access-Control-Allow-Origin": "https://api.springrole.com",
                "Access-Control-Allow-Methods":"GET",
                    "Access-Control-Allow-Credentials": true
        }
    }).success(function(data, status, headers, config) {
        $scope.details.response = JSON.stringify(data);

        console.log("success" + data)
    })

如果我删除请求中的 header ,我会收到下面显示的错误

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

任何建议或解决方法将不胜感激。

最佳答案

是的,您遇到了 Same Origin Policy这是出于安全原因。简而言之,基本上你是在请求/打开来自不同来源的东西,这不是很安全。如果需要,您可以通过添加参数 --disable-web-security 在 Chrome 中暂时禁用它。

所以你可以从终端运行

open -a Google\Chrome --args --disable-web-security

我经常这样做,所以我最后只是在我的 .bash_profile 中添加别名,这样我就可以轻松访问它

# Run Insecure Chrome
alias insecure-chrome='open -a Google\ Chrome --args --disable-web-security'

然后在终端中我只需调用 insecure-chrome Chrome 就会弹出。如果您在顶部看到安全警告,您就会知道它有效。警告看起来像这样

enter image description here

当然,在执行此操作时必须小心警告。我不会以这种模式上网,但在本地工作应该没问题。

对于 Windows 用户,您可以导航到安装和设置 Chrome 的文件夹

chrome.exe --user-data-dir="C:/temp" --disable-web-security

确保在执行此操作之前关闭所有 Chrome 实例,否则它将无法工作。

关于angularjs - 请求被重定向到 'https://..com/site/login?' ,这对于需要预检的跨源请求是不允许的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28036696/

相关文章:

javascript - Chrome 中带参数的 Jquery addEventListener

linux - 如何捕获和存储从我的服务器发送的传出 HTTP 请求?

iPhone Mobile Safari,最大并行 http 连接数是多少?

google-app-engine - 我可以拥有的最大 "POST"大小是多少?

javascript - 如何使用 $http 为 Rest API 调用创建通用服务?

javascript - AngularJs 事件 $stateChangeStart 未触发

javascript - Firefox 浏览器缓慢加载 AngularJS 应用程序

javascript - 如何强制 URL 参数出现在 ui-router 的 URL 中?

angularjs - 什么是基于 DOM 的模板?

javascript - 使用扩展程序的 Chrome 自动化;点击按钮和填写表格