如何在Netlify部署上启用CORS? [英] How to enable CORS on a Netlify deployment?

查看:117
本文介绍了如何在Netlify部署上启用CORS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Netlify提供一些静态 .json 文件。它们在浏览器中的加载情况很好,但是当我尝试通过javascript提取它们时,在控制台中出现以下错误:

I'm using Netlify to serve some static .json files. They load fine in the browser but when I try to fetch them via javascript I get the following error in the console:


访问地址为(从重定向)来自源
'http:// localhost:3000'已被CORS策略阻止:所请求的
资源上没有
'Access-Control-Allow-Origin'标头。如果不透明的响应满足您的需求,请将请求的
模式设置为 no-cors以在禁用CORS的情况下获取资源。

Access to fetch at (redirected from ) from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

I在Netlify仪表板中检查了我网站设置中的所有选项,但没有看到任何启用CORS的选项,该怎么做?

I checked all the options in my site's settings in the Netlify dashboard and don't see any options to enable CORS, how can this be done?

推荐答案

在index.html旁边添加一个名为 _headers 的文件,其内容如下:

Add a file called _headers next to your index.html with the following content:

/*
  Access-Control-Allow-Origin: *

最好将其更改为实际值而不是生产中的 *

It's better to change it your actual origin instead of * in production.

这篇关于如何在Netlify部署上启用CORS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆