You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PocketBase JavaScript SDK向自定义路由发送请求?

使用PocketBase SDK原生方法发送POST请求到自定义路由的问题

我正尝试从Svelte前端出发,找到一种使用PocketBase SDK原生方式向PocketBase服务器发送POST请求的方法。

后端代码(main.go):

app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
    e.Router.AddRoute(echo.Route{
        Method: http.MethodPost,
        Path: "/api/myRoute",
        Handler: func(c echo.Context) error {
            // 自定义逻辑:通过参数计算custom_value
            custom_value := "cool_value"
            return c.String(200, custom_value)
        },
    })
    return nil
})

我希望实现类似如下的前端代码:

前端示例代码(Frontend.ts):

onMount(async () => {
    // 根据变量example向服务器请求信息
    const example = "test";
    const example_reply = await pb.route(POST, '/api/myRoute', /* 这里需要指定example是POST请求的表单值 */ example);
    // 使用返回结果
    if (example_reply === "cool_value") {
       console.log('得到了预期结果');
    } else {
       console.log('出错了!!!');
    }
})

请问我想要的这种SDK原生方法是否存在?还是只能使用原生JavaScript的fetch来请求自定义路由?

我已经试过用原生JavaScript发送POST请求,这种方式可行但没有用到PocketBase包:

const response = await fetch('/api/example', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ example: "test" }), // 将输入转为JSON
      });

内容的提问来源于stack exchange,提问作者Salad Iche

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 09:14:57