请求被重定向到 'https://..com/site/login?',这对于需要预检的 cross-origin 请求是不允许的
The request was redirected to 'https://..com/site/login?', which is disallowed for cross-origin requests that require preflight
在发布这个问题之前,我已经完成了大部分堆栈溢出问题,但我没有得到答案
我正在尝试访问将执行 Oauth 身份验证的客户端 api,因此当我调用 api 它应该抛出一个弹出窗口 window 以进行类似于 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)
})
如果我删除请求中的 headers,我将收到下面显示的错误
No 'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost:8080' is therefore not allowed
access.
如有任何建议或解决方法,我们将不胜感激。
是的,您正在 运行 进入 Same Origin Policy,这是出于安全原因而设立的。简而言之,基本上你是 requesting/opening 来自不同来源的东西,这不是很安全。如果需要,您可以通过添加参数 --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 会弹出。如果您在顶部看到安全警告,您就会知道它有效。警告看起来像这样
并且当然,在执行此操作时必须小心警告。我不会在这种模式下上网,但在本地工作应该没问题。
对于 windows 用户,您可以导航到安装 Chrome 的文件夹并设置
chrome.exe --user-data-dir="C:/temp" --disable-web-security
确保在执行此操作之前关闭所有 Chrome 个实例,否则它将无法工作。
在发布这个问题之前,我已经完成了大部分堆栈溢出问题,但我没有得到答案
我正在尝试访问将执行 Oauth 身份验证的客户端 api,因此当我调用 api 它应该抛出一个弹出窗口 window 以进行类似于 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)
})
如果我删除请求中的 headers,我将收到下面显示的错误
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
如有任何建议或解决方法,我们将不胜感激。
是的,您正在 运行 进入 Same Origin Policy,这是出于安全原因而设立的。简而言之,基本上你是 requesting/opening 来自不同来源的东西,这不是很安全。如果需要,您可以通过添加参数 --disable-web-security
.
因此您可以从终端 运行
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 会弹出。如果您在顶部看到安全警告,您就会知道它有效。警告看起来像这样
并且当然,在执行此操作时必须小心警告。我不会在这种模式下上网,但在本地工作应该没问题。
对于 windows 用户,您可以导航到安装 Chrome 的文件夹并设置
chrome.exe --user-data-dir="C:/temp" --disable-web-security
确保在执行此操作之前关闭所有 Chrome 个实例,否则它将无法工作。