端点上不允许使用 Sveltekit POST 方法

MCC*_*MCC 5 javascript endpoint fetch-api vite sveltekit

我正在尝试让这个 fetch 方法在 Svelte 中工作

当 page.svelte 调用函数来获取数据时,在控制台中我收到此消息

[HTTP/1.1 405 Method Not Allowed 19ms]
Run Code Online (Sandbox Code Playgroud)

我已经缩小到这个范围

POST method not allowed
Run Code Online (Sandbox Code Playgroud)

我需要在配置文件中设置一个变量还是有另一个我缺少的解决方案?

// svelte.config.js

import adapter from '@sveltejs/adapter-auto';

const config = {
    kit: {
        adapter: adapter(),
        methodOverride: {
            allowed: ['POST']
        },
    }
};

export default config;
Run Code Online (Sandbox Code Playgroud)

这两个文件都位于 Routes 文件夹中

// 获取数据.js

export const POST = async(data) => {
  const response = // get data function here
  return {
    body: {
        data: response
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

// page.svelte

async function fetchData() {
   const response = await fetch('./fetch-data', {
     method: 'POST',
     body: JSON.stringify(),
     headers: {
       'content-type': 'application/json'
     }
   })
   const { data } = await response.json()
   return data
}
Run Code Online (Sandbox Code Playgroud)

MCC*_*MCC 3

POST解决方案是在 get-data.js 文件中将变量名称更改为小写

  • 如果您的意思是“export const POST”行,您应该更新您的 SvelteKit,最近已更改为使用大写 (4认同)