我正在运行一个本地run服务器,它向ngrok服务器运行XHR请求,也是从我的PC运行的。
我去叫XMLHttpRequest cannot load http://foo.ngrok.io/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access。
ngrok常见问题似乎提到了CORS头文件,但只与基本身份验证相关-它没有提到如何设置头文件以便我可以在开发中测试我的应用程序。
如何更改ngrok的CORS选项以允许从localhost加载请求?
发布于 2018-08-16 22:20:11
我今天偶然发现了这个问题,并通过启动ngrok并包含-host-header标志解决了这个问题。
ngrok http -host-header=rewrite 3000
从文档中:
使用
-host-header开关重写传入的HTTP请求。
如果指定了rewrite,则主机标头将被重写以匹配转发地址的主机名部分。
发布于 2017-07-19 16:07:02
首先,ngrok只是一个隧道,而不是一个服务器,所以在ngrok中配置CORS头是完全不可能的。因此,任何类型的CORS配置都需要在服务器级别完成。
例如,如果您使用的是nginx服务器,则需要在nginx conf文件中配置头部,如下所示:
location / {
/*some default configuration here*/
add_header 'Access-Control-Allow-Origin' '*';
}通过添加此标头,您可以说允许从任何地址跨域访问您的地址,因为标头的值是'*‘。您还可以通过替换该值来指定允许访问您的地址的特定地址。
发布于 2022-01-19 17:05:49
让ngrok工作花了一些时间,但实际上很容易。
In chrome there is an option to turn off CORS. In the chrome address bar go to
chrome://flags and look for Block insecure private network requests.
This needs to be disabled.
Second, in my ajax request I had used an absolute path and this needed to be changed
to a relative path. 请记住:这是为了运行localhost并将其暴露给web
https://stackoverflow.com/questions/40327672
复制相似问题