为什么它被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:它没有 HTTP 正常状态。?



我遵循了以下指南:

https://serverless.com/blog/cors-api-gateway-survival-guide/

使用serverless、lambda、nodejs创建后端服务。如果使用poster进行测试,则端点工作正常。

在请求标头下面,它们是如何设置的(为了找出问题,我尝试启用了所有功能(。

General:
Request Method: OPTIONS
Status Code: 400 
Remote Address: xxx.xxx.xxx.xxx:443
Referrer Policy: no-referrer-when-downgrade
Response Headers:
access-control-allow-credentials: true
access-control-allow-headers: Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Access-Control-Allow-Origin, Access-Control-Allow-Credentials
access-control-allow-origin: *
content-length: 97
content-type: application/json
date: Fri, 08 Nov 2019 16:26:15 GMT
status: 400
via: 1.1 xxxxxxx.cloudfront.net (CloudFront)
x-amz-apigw-id: xxxxxxxx=
x-amz-cf-id: xxxxxx-EFTU2MF4k_TQ==
x-amz-cf-pop: xxxxx-C1
x-amzn-requestid: xxxxxxx-85a7-4d61-bf42-081944a4d307
x-amzn-trace-id: Root=1-xxxxxxx-37555e2ccf9ab35021c26f98;Sampled=0
x-cache: Error from cloudfront
Request headers
:authority: xxxxxx.execute-api.eu-west-1.amazonaws.com
:method: OPTIONS
:path: /stg/getConfigUrl
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9,it;q=0.8
access-control-request-headers: access-control-allow-headers,access-control-allow-methods,access-control-allow-origin,content-type
access-control-request-method: POST
cache-control: no-cache
origin: http://0.0.0.0:8017
pragma: no-cache
referer: http://0.0.0.0:8017/
sec-fetch-mode: cors
sec-fetch-site: cross-site
user-agent: Mozilla/5.0 ....

我认为已经设置了所需的飞行前请求标头。我不明白这个错误。"它没有HTTP正常状态"的真正含义是什么?

尝试使用Postman(相同的请求(,它可以回答HTTP200。但通过浏览器,OPTION请求似乎回答了400…为什么?我该怎么解决这个问题?

基本上,在你访问的网站中编码正确的XHR请求之前,你的浏览器会问"好吧,我在localhost(或web.com(",然后它会问请求指向的网站"嘿,我有什么选项可以打电话给你?",如果网站配置良好,它可以回答例如"所以,我允许来自collsite.com的请求,但只有当你想GET或POST时,我才会允许你向我发送你想要的所有HTTP头"和你的浏览器"好吧,好吧!我会检查这个请求并为你执行这些规则!"然后它会抛出或不抛出已知的"CORS错误"。从技术上讲,它首先会用动词"OPTIONS"做一个空请求,指向请求所指向的站点。之后,它将"捕获"特定的HTTP标头并检查其内容,在这里进行"对话"。例如,允许域调用的"Access Control Allow Origin"或允许域使用的"Access Control-Allow Methods"方法,等等,这些标头和其他标头负责允许或不允许已知的"CORS"。

相关内容

最新更新