跳至主内容
版本:9.x

跨域发送 Cookie

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

如果您的 API 与前端处于不同的源(origin),并且希望发送 Cookie,那么您需要在服务器上启用 CORS,并通过在 fetch 中提供选项 {credentials: "include"} 来在请求中发送 Cookie。

可以按照以下方式修改 tRPC 所使用的 fetch 函数的参数。

app.ts
ts
import { createTRPCClient } from '@trpc/client';
const client = createTRPCClient<AppRouter>({
url: 'YOUR_SERVER_URL',
fetch(url, options) {
return fetch(url, {
...options,
credentials: 'include',
});
},
});
app.ts
ts
import { createTRPCClient } from '@trpc/client';
const client = createTRPCClient<AppRouter>({
url: 'YOUR_SERVER_URL',
fetch(url, options) {
return fetch(url, {
...options,
credentials: 'include',
});
},
});