简体   繁体   English

Javascript CORS 请求,不存在带有 200 状态代码的“Access-Control-Allow-Origin”标头

[英]Javascript CORS request, No 'Access-Control-Allow-Origin' header is present with 200 status code

I am facing the similar issue as Javascript CORS - No 'Access-Control-Allow-Origin' header is present .我面临着与Javascript CORS类似的问题- No 'Access-Control-Allow-Origin' header is present Unfortunately, I do not understand the answer and want to understand the root cause.不幸的是,我不明白答案并想了解根本原因。

High level data flow:高级数据流:

Javascript/HTML(Deployed in S3) -> AWS API Gateway. Javascript/HTML(部署在 S3 中)-> AWS API 网关。 Gateway will return the data back.网关将返回数据。

Here is my understanding and the fact:这是我的理解和事实:

Since my javascript code is in S3, I have to make CORS request to API Gateway to fetch the data.由于我的 javascript 代码在 S3 中,因此我必须向 API Gateway 发出 CORS 请求以获取数据。 From the code perspective, there is nothing special between CORS request and Same-Origin Request.从代码的角度来看,CORS 请求和同源请求之间没有什么特别之处。

Also my code will actually make the request the api gateway and get 200 status code(found it in Network tab - Chrome).此外,我的代码实际上会将请求发送到 api 网关并获得 200 状态代码(在“网络”选项卡 - Chrome 中找到它)。 However, from the console tab - Chrome I am getting No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<placeholder>' is therefore not allowed access.但是,从控制台选项卡 - Chrome 我收到No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<placeholder>' is therefore not allowed access. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<placeholder>' is therefore not allowed access.

JavaScript Code: JavaScript 代码:

var xhr = new XMLHttpRequest();
xhr.open('GET', URL);
xhr.send();  

My Questions:我的问题:

  1. First, From the code perspection, there is nothing special between CORS request and Same-Origin request.首先,从代码的角度来看,CORS请求和Same-Origin请求之间没有什么特别之处。 Is this correct?这样对吗?
  2. From my understanding, When making CORS request, we actually make 2 requests.据我了解,在提出 CORS 请求时,我们实际上提出了 2 个请求。 the first one is to make a request to OPTIONS method to make sure we are able to make the actual request.第一个是向 OPTIONS 方法发出请求,以确保我们能够发出实际请求。 If the server side returns 'Access-Control-Allow-Origin': '*'.如果服务器端返回'Access-Control-Allow-Origin':'*'。 then the second request can be sent.然后可以发送第二个请求。 Is this correct?这样对吗?
  3. If the above 1,2 are correct, Do I miss anywhere?如果上述 1,2 是正确的,我会错过任何地方吗? I have already configured the API gateway to allow CORS and tested it.我已经将 API 网关配置为允许 CORS 并对其进行了测试。 Its returning the response header {"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"GET,OPTIONS","Access-Control-Allow-Headers":"Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token","Content-Type":"application/json"}它返回响应头{"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"GET,OPTIONS","Access-Control-Allow-Headers":"Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token","Content-Type":"application/json"}

Eventually, figure this out!最终,弄清楚这一点!

First, The tec knowledge for CORS request is correct from my understanding.首先,根据我的理解,CORS 请求的技术知识是正确的。 Basically, When we need a CORS, then the response header from server side should have基本上,当我们需要 CORS 时,服务器端的响应头应该有

{"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"GET,OPTIONS","Access-Control-Allow-Headers":"Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token","Content-Type":"application/json"} . {"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"GET,OPTIONS","Access-Control-Allow-Headers":"Content-Type,X-Amz-日期、授权、X-Api-Key、X-Amz-Security-Token","Content-Type":"application/json"} 。

But what happened to my case?但是我的案子怎么了?

I am using api gateway as server and enable CORS but I am not aware of the issue that I am integrating my api gateway with lambda func.我使用 api 网关作为服务器并启用 CORS,但我不知道我将 api 网关与 lambda func 集成的问题。 In this case, Our lambda func must return the response with correct header.在这种情况下,我们的 lambda func 必须返回带有正确标头的响应。

Solution?解决方案?

Adding the header mentioned above as the part of the return of lambda func.添加上面提到的标题作为 lambda func 返回的一部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CORS没有&#39;Access-Control-Allow-Origin&#39;标头存在,但OPTIONS和POST请求却得到200,但 - CORS Getting No 'Access-Control-Allow-Origin' header is present but OPTIONS and POST request get 200 but CORS header 如果 http 状态代码不是 200,则缺少“Access-Control-Allow-Origin” - CORS header ‘Access-Control-Allow-Origin’ missing if http status code is not 200 CORS标头“ Access-Control-Allow-Origin”丢失,但它出现在标头中 - CORS header 'Access-Control-Allow-Origin' missing but it present in the header CORS所请求的资源上没有“ Access-Control-Allow-Origin”标头 - CORS No 'Access-Control-Allow-Origin' header is present on the requested resource Web API 2 CORS不存在“ Access-Control-Allow-Origin”标头 - web api 2 CORS No 'Access-Control-Allow-Origin' header is present CORS“没有&#39;Access-Control-Allow-Origin&#39;标题存在”但是有 - CORS “No 'Access-Control-Allow-Origin' header is present” yet there is CORS-不存在“ Access-Control-Allow-Origin”标头 - CORS - No 'Access-Control-Allow-Origin' header is present Javascript XMLHttpRequest-已被CORS策略阻止:所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - Javascript XMLHttpRequest - has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource 已被 CORS 策略阻止:Javascript 中请求的资源上不存在“Access-Control-Allow-Origin”标头 - has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource in Javascript django &amp; javascript fetch():CORS 政策:没有“访问控制允许来源”Z099FB995346F31C79E3ZF6 存在 - django & javascript fetch(): CORS policy: No 'Access-Control-Allow-Origin' header is present
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM