简体   繁体   English

PUT方法的CORS访问错误,弹性beantalk上托管的AWS上的api

[英]CORS access error for PUT method, api on aws hosted on elastic beanstalk

I've deployed an api on AWS API Gateway using http custom integration 我已经使用http自定义集成在AWS API Gateway上部署了一个api

I've enabled CORS as seen below: 我已启用CORS,如下所示:

启用的cors

For both GET and PUT methods, I am getting the following error: "url from origin [my origin] has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource" 对于GET和PUT方法,我都收到以下错误:“来自起源的URL [我的起源]已被CORS策略阻止:所请求的资源上没有'Access-Control-Allow-Origin'标头”

Code: 码:

 fetch(url, {
  method: 'POST', 
  mode: "cors",
  headers: {
    'Content-Type': 'application/json',
    'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token' //copied from screenshot above 
  },
  body: JSON.stringify(data)
})
.then(response => console.log('Success:', response))
.catch(error => console.error('Error:', error));
}

Access-Control-Allow-Headers are set in the server, you should remove it from your code. 在服务器中设置了Access-Control-Allow-Headers ,您应该将其从代码中删除。 It won't make any difference if you put it in your request. 如果将其放入请求中,则没有任何区别。

Is your API Gateway method protected by Lambda Authorizer or configured to use an API Key? 您的API网关方法是否受Lambda Authorizer保护或配置为使用API​​密钥? If so, request might be rejected by one of them and response won't include Access-Control-Allow-Origin header so browser's preflight check will be failed. 如果是这样,请求可能会被其中之一拒绝,并且响应不会包含Access-Control-Allow-Origin标头,因此浏览器的预检检查将失败。

You also need to check what response is being returned by API method integration. 您还需要检查API方法集成正在返回什么响应。 In case of Proxy Lambda, response should include Access-Control-Allow-Origin header. 如果是代理Lambda,则响应应包括Access-Control-Allow-Origin标头。

Take a look at integration response documentation . 看一下集成响应文档

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM