reactjs - 美居码头无法正确配置 CORS 问题
问题描述
我尝试了几个小时来使用 docker 配置我的 Mercure,但未能解决 corss 问题:
已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头
我有这个 docker-compose 文件
提炼
mercure:
image : dunglas/mercure
volumes:
- ./mercure/caddy:/etc/caddy
ports:
- "8081:80"
- "443:443"
environment:
- SERVER_NAME=localhost
- MERCURE_SUBSCRIBER_JWT_KEY=changeIt
- MERCURE_PUBLISHER_JWT_KEY=changeIt
- MERCURE_CORS_ALLOWED_ORIGINS=*
- ALLOW_ANONYMOUS=1
而这个 Caddyfile :
# The address of your server
localhost
:80
route {
mercure {
# Publisher JWT key
publisher_jwt changeIt
# Subscriber JWT key
subscriber_jwt changeIt
cors_origins http://localhost:3000
}
respond "Not Found" 404
}
我在客户端的请求在 React 中
http.get('/api/mercure').then( response => {
this.setCookie( response.data.token);
const url = new URL('http://localhost:8081/.well-known/mercure');
url.searchParams.append('topic', 'http://agora.org/votes/{id}');
const eventSource = new EventSource(url, {withCredentials: true});
eventSource.onmessage = (data) => {
console.log(data);
}
},error => {
console.log( error);
})
有人可以帮我解决这个问题吗?提前谢谢
解决方案
我已经成功地使用了docker-compose
没有 CORS 问题的 Mercure。你可以在这里找到实现。
正如我到达这里,其他人也可能到达这里,也可能帮助那个人。
推荐阅读
- sql - 甲骨文 SQL | 默认情况下以 null 作为身份生成的共享
- javascript - 将异步 es6 语法函数转换为 es5 语法异步函数
- angular - Angular forkJoin 订阅未触发
- docker - 使用 shell 脚本将文件从容器复制到本地
- bash - 使用 sed/grep 从 markdown 文件中提取图像 URI,其中包含单行中的重复项
- python - 使用 pandas 比较单元迭代
- swift - 如何从 SwiftUI 中的形状创建 NavigationLink 或 Button,仅通过点击形状而不是其框架触发
- python - 是否有实现新的 MySQL v8+ TRUE 异步功能的 Python MySQL 库?
- python - 如何在熊猫中将字符串转换为弧度以计算两点之间的距离
- assembly - 程序集 8086 - x86 中的减法