gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 15:23:48 32 4
gpt4 key购买 nike

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

我正在尝试访问一个将执行 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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com