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

如何在SvelteKit(TypeScript)中提取actix-web后端返回的Cookie

问题场景

使用 TypeScript 开发 SvelteKit 项目时,需要提取 actix-web 后端返回的 Cookie 值,且要求不直接操作 DOM。尝试过 js-cookie、网络搜索及 ChatGPT 给出的方案,均未解决问题。网络面板中可看到 Set-Cookie 响应头,但该 Cookie 既非 Secure 也非 HttpOnly。

解决方案

在 actix-web 后端设置 Cookie 时,指定 domain 为 localhost,同时开启 Secure 属性,问题得以解决。

actix-web 端示例代码

use actix_web::{http::header::Cookie, HttpResponse};

fn set_session_cookie() -> HttpResponse {
    let cookie = Cookie::build("session_id", "your_session_value")
        .domain("localhost")
        .secure(true)
        .finish();

    HttpResponse::Ok()
        .cookie(cookie)
        .body("Cookie configured successfully")
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 14:30:56