【发布时间】:2018-12-30 22:12:25
【问题描述】:
我正在尝试从已部署的反应应用程序 (AWS S3 CDN) 对 grails 应用程序上的资源进行休息调用。
React 应用具有 ULR https://something.cloudfront.net/index.html
CORS 配置看起来与 AWS guide 上的配置相同,当然除了 exemple.com。
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>https://example.com</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<ExposeHeader>ETag</ExposeHeader>
<ExposeHeader>x-amz-meta-custom-header</ExposeHeader>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
Grails 应用配置 (application.yml) 如下所示:
grails:
cors:
enabled: true
allowedOrigins:
- "https://something.cloudfront.net"
通过所有这些设置,我在浏览器中收到消息:
未能加载https://example.com/someResources:响应 预检请求未通过访问控制检查: “Access-Control-Allow-Origin”标头有一个值 'http://localhost:52449' 不等于提供的原点。 因此不允许使用原点“https://something.cloudfront.net” 访问。
我真的很困惑,为什么我会看到 http://localhost:52449?我已经构建了react应用程序并将静态文件复制到CDN中,http://localhost:52449是react应用程序在本地以npm start启动时的地址。
进行 rest 调用的 React 代码如下所示:
axios.get<any>('https://example.com/someResources',
{
withCredentials: true,
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"X-Requested-With": "XMLHttpRequest"
},
})
.then((response) =>
console.log(response);
);
编辑 1:
我做错了什么,因此 CORS 问题仍然存在?
【问题讨论】:
标签: reactjs amazon-web-services grails cors cdn