Sveltekit端点总是返回200个状态

发布于 2025-02-13 11:53:45 字数 553 浏览 1 评论 0原文

我正在使用Sveltekit,并且有端点问题。我要发布到一个端点,即使具有400个身份,它也总是返回200个状态。

login.svelte

const res = await fetch('login', {
    method: 'POST',
    body: JSON.stringify({
        email,
        redirect: `${window.location.origin}/app/dashboard`
    })
});

端点-Login.js

export const post = async ({request}) => {

  console.log("Inside request");
  
  return {
    status: 400
  };
}

通过控制台日志所看到的端点被击中,但状态在响应对象中返回200。

I am using SvelteKit and am having an issue with endpoints. I am posting to an endpoint and it's always returning a 200 status, even with a hardcoded 400 status.

Login.svelte

const res = await fetch('login', {
    method: 'POST',
    body: JSON.stringify({
        email,
        redirect: `${window.location.origin}/app/dashboard`
    })
});

ENDPOINT - login.js

export const post = async ({request}) => {

  console.log("Inside request");
  
  return {
    status: 400
  };
}

The endpoint is being hit as seen through a console log, but the status is returned 200 in a Response object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

烟酒忠诚 2025-02-20 11:53:45

问题是微妙的。但这最终很有意义。

如果我们阅读 docs> 。具体来说:

由于页面和路由具有相同的URL,因此您需要包括一个接受:应用程序/JSON标题以从端点获取JSON,而不是从页面上获得HTML。

因为您使用的是页面端点,所以(而不是独立端点)Sveltekit有2条用于POST /login < /code>的路由。一个返回html页面(login.svelte)的人,一个返回JSON(login.js中定义的页面端点)。

因此,我们需要做的是告诉服务器,我们期望在application/json而不是text/*中期望结果(后者似乎是获取的默认值,这似乎是) 。这样,服务器知道要返回的路线。

有2个解决方案:1。更新您的获取语句以包括Accept标题;或2:将您的登录名称重命名为也不是路由的东西,因此服务器不会感到困惑。

我个人更喜欢第一个,因为它在技术上更正确。因此,您的login.svelte函数将成为:

const res = await fetch('/login', {
  method: 'POST',
  headers: { 'accept': 'application/json' }, //Added the header here
  body: JSON.stringify({
    email,
    redirect: `${window.location.origin}/app/dashboard`
  })
})

The problem is subtle. But it makes sense in the end.

If we read the docs carefully, we can figure out what's going on. Specifically:

Because the page and route have the same URL, you will need to include an accept: application/json header to get JSON from the endpoint rather than HTML from the page.

Because you are using page endpoints, (rather than standalone endpoints) Sveltekit has 2 routes for POST /login. One that returns the HTML page (the login.svelte), and one that returns the JSON (the page endpoint defined in login.js).

So what we need to do is tell the server that we are expecting the result in application/json rather than text/* (the latter is the default for fetch it seems). This way the server knows which route to return.

There are 2 solutions to this: 1. Update your fetch statement to include the accept header; or 2: Rename your login.js to something that isn't also a route, so the server doesn't get confused what to return.

I personally prefer the first one as it's technically more correct. So then your login.svelte function would become:

const res = await fetch('/login', {
  method: 'POST',
  headers: { 'accept': 'application/json' }, //Added the header here
  body: JSON.stringify({
    email,
    redirect: `${window.location.origin}/app/dashboard`
  })
})

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