Svelte(不是Sveltekit)添加CORS“允许来源...”在客户端HTTP标头中
因此,我一直存在错误:
访问'http:// localhost:8000/< ...>'从原点'http:// localhost:8080'被CORS策略阻止:在请求的资源上没有“访问控制”标题。如果不透明的响应满足您的需求,请将请求模式设置为“无库”,以通过禁用CORS来获取资源。
我想知道如何在Svelte网站的HTTP标头中添加字段access-control-Allow-Origin
。我再次确定它,我在Svelte上,而不是在Sveltekit上(Sveltekit的很多解决方案) 我会认为可以在main.ts
中添加它,但是如果这样做,我会遇到错误:
const app = new App({
target: document.body,
props: {
name: 'my APP'
},
headers: {
'Access-Control-Allow-Origin': '*' // or, e.g. replacing * by http://localhost:8000
}
});
而且我会得到错误:
Argument of type '{ target: HTMLElement; props: { name: string; }; headers: { 'Access-Control-Allow-Origin': string; }; }' is not assignable to parameter of type 'Svelte2TsxComponentConstructorParameters<any>'.
Object literal may only specify known properties, and 'headers' does not exist in type 'Svelte2TsxComponentConstructorParameters<any>'.ts(2345)
那么,我可以在哪里设置?
So I keep having the error:
Access to fetch at 'http://localhost:8000/<...>' from origin 'http://localhost:8080' 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 would like to know how to add the field Access-Control-Allow-Origin
in the HTTP Header for a Svelte website. I precise it again, I am on Svelte, not on SvelteKit (Foud lots of solution for SvelteKit)
I would have think it could be added in the main.ts
but I get an error if I do so :
const app = new App({
target: document.body,
props: {
name: 'my APP'
},
headers: {
'Access-Control-Allow-Origin': '*' // or, e.g. replacing * by http://localhost:8000
}
});
And I get the error:
Argument of type '{ target: HTMLElement; props: { name: string; }; headers: { 'Access-Control-Allow-Origin': string; }; }' is not assignable to parameter of type 'Svelte2TsxComponentConstructorParameters<any>'.
Object literal may only specify known properties, and 'headers' does not exist in type 'Svelte2TsxComponentConstructorParameters<any>'.ts(2345)
So, where can I set that ? ????
NB. I also found this about CORS with NodeJS, can I use it with Svelte, or it will not work ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Cors与Svelte无关。服务器必须允许从另一个来源而不是客户端访问。因此,无论使用什么服务
http:// localhost:8000
必须设置标题。如何完成操作取决于服务器的类型。可以从reque下载的示例使用
sirv-cli
预览编译的Svelte文件。要使用CORS标头进行SIRV返回请求,可以使用FLAG-c
,仅需要添加到start> start
script中的package.json 。
CORS has nothing to do with Svelte; the server has to allow access from another origin, not the client. So whatever is serving
http://localhost:8000
has to set the headers. How exactly that is done depends on the type of server.The example that can be downloaded from the REPL uses
sirv-cli
to preview the compiled Svelte files. To make Sirv return requests with CORS headers, the flag-c
can be used which just needs to be added to thestart
script inpackage.json
.