我正在使用脚手架站点和静态子域来提供我的静态文件。 使用我正在使用我的 yesod 应用程序服务的 Javascript 游戏的显式 url 请求静态文件(使用 Phaser 框架编写)。
来自 Javascript 控制台的示例错误消息是这样的:
Phaser.Loader error loading file: player1_icon from URL http://example.com/static/games/mygame/images/player1/icon.png
Image from origin 'http://example.com' has been blocked from loading by Cross-Origin Resource Sharing policy:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
一些研究表明,在处理函数中添加行 addHeader "Access-Control-Allow-Origin""*"
可以解决这个问题。然而,静态路由不使用处理函数,所以我不知道我应该把它放在哪里。
如有任何帮助,我们将不胜感激。
最佳答案
这可以通过使用添加此 header 的 WAI 中间件来完成。该中间件将添加到您的 Application
模块中。 wai-extra 包中有许多中间件可以用作如何执行此操作的示例。
更一般地说:这可能是一个值得构建到 yesod-static 本身的特性。你能打开一个关于它的 Github 问题吗?
关于file - 如何在 Yesod 中为静态文件启用 'Access-Control-Allow-Origin' header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26824120/