代码之家  ›  专栏  ›  技术社区  ›  kittu

无法使用axios发送cookie

  •  -1
  • kittu  · 技术社区  · 2 年前

    尝试添加 {withCredentials: true} 但仍然无法将cookie发送到后端

    return await axios.post(`${API_URL}/posts/category/${id}`, {
      withCredentials: true
    })
    

    enter image description here

    曲奇

    enter image description here

    2 回复  |  直到 2 年前
        1
  •  1
  •   Phil    2 年前

    问题是您正在发送 { withCredentials: true } 对象作为的第二个参数 axios.post() 这个 signature 对于该函数

    axios.post(url[,data[,config]])

    通常使用您想要发送的POST请求 一些 数据,但如果您真的想跳过它,请通过 undefined 作为第二个参数

    return axios.post(`/posts/category/${id}`, undefined, {
      baseURL: API_URL,
      withCredentials: true,
    });
    

    我强烈建议利用Axios的功能,创建具有方便默认值的自定义实例

    const apiClient = axios.create({
      baseURL: API_URL,
      withCredentials: true,
    });
    
    // ...
    
    return apiClient.post(`/posts/category/${id}`); // much easier
    
        2
  •  -2
  •   Minh Quang Lê    2 年前

    我在网上找到了这个答案,希望对你有所帮助。 自2020年以来,Chrome对跨域cookie设置添加了更多令人讨厌的限制,您必须将SameSite的cookie设置为无,否则Chrome将拒绝发送cookie。此外,如果您设置SameSite,则必须设置安全。

    proxy_cookie_path / "/; secure; SameSite=none";