【问题标题】:how to send cookies with axios post method using react Js如何使用 React Js 使用 axios post 方法发送 cookie
【发布时间】:2019-12-27 11:11:08
【问题描述】:

我将 react js 用于前端,将 spring boot 用于后端进程。我需要将 cookie 从前端发送到后端,为此我正在尝试使用 axios 的以下方法:

前端

async function submitPageData() {
    try {
      const jwttoken = {
        headers: {
          jwt: to12,
          Cookie: token
        }
      };
      const response = await axios
        .put(
          url,
          {
            id: pageId,
            projectName: PROJECT_NAME,
            title: title
          },
          jwttoken
        )
        .then(function () {

       });
    } catch (error) {
        }
      }
    }
  }

并使用@CookieValue 注释在后端接收 cookie,但我检查并发现我的请求标头没有携带 Cookie。

请指导我,如何从 react js axios 方法发送 cookie,以便我能够在后端接收它。

编辑

const jwttoken = {
        headers: {
          jwt: to12,
          Cookie: token,
          withCredentials: true
        }

提前致谢!

【问题讨论】:

  • 我已经在我的标题中尝试过这个东西,因为编辑在问题本身中更新,但请求标题中仍然没有 cookie 的迹象。

标签: reactjs spring-boot cookies axios


【解决方案1】:

出于某种原因,使用withCredentials 作为标题也不适用于我。

这样添加它:

import axios from "axios";
axios.defaults.withCredentials = true;

【讨论】:

  • 感谢您的回复。我也尝试过这种方法,但使用这种方法,我的 axios 仅发送选项请求并获得 200 作为响应,但在选项请求之后,它可以发送 put 请求。
  • 另外,它已经停止了应用程序的正常流程。现在,evevy 页面需要多次点击才能成功加载。
猜你喜欢
  • 2021-11-03
  • 2019-11-24
  • 2023-03-28
  • 1970-01-01
  • 2018-10-01
  • 2021-09-29
  • 2022-01-15
  • 2019-02-12
  • 2020-11-30
相关资源
最近更新 更多