Svelte(不是Sveltekit)添加CORS“允许来源...”在客户端HTTP标头中

发布于 2025-02-12 16:10:47 字数 1068 浏览 0 评论 0原文

因此,我一直存在错误:

访问'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-Originin 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情感失落者 2025-02-19 16:10:47

Cors与Svelte无关。服务器必须允许从另一个来源而不是客户端访问。因此,无论使用什么服务http:// localhost:8000必须设置标题。如何完成操作取决于服务器的类型。

可以从reque下载的示例使用 sirv-cli 预览编译的Svelte文件。要使用CORS标头进行SIRV返回请求,可以使用FLAG -c,仅需要添加到start> start script中的package.json 。

{
  ...
  "scripts": {
    ...
    "start": "sirv public --no-clear -c"
  },
}

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 the start script in package.json.

{
  ...
  "scripts": {
    ...
    "start": "sirv public --no-clear -c"
  },
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文