小编Gav*_*nry的帖子

带有 axios 的 X-XSRF-TOKEN 标头

X-XSRF-TOKEN如果我设置了XSRF-TOKENcookie 服务器端,我是否必须设置任何东西来发送标头?

https://github.com/axios/axios/blob/master/lib/defaults.js#L74 https://github.com/axios/axios/blob/master/dist/axios.js#L1072

它读起来好像我没有,但我没有看到有人出去。

我要补充一点,我已设置withCredentials为 true,因此我确实遇到了 OR 中的第一次检查:

var xsrfValue = (config.withCredentials || isURLSameOrigin(config.url)) && config.xsrfCookieName ?
            cookies.read(config.xsrfCookieName) :
            undefined;

          if (xsrfValue) {
            requestHeaders[config.xsrfHeaderName] = xsrfValue;
}
Run Code Online (Sandbox Code Playgroud)

所以如果config.xsrfCookieName是默认.....

更新:

所以,我的OPTIONS预检CORS工作和POST现在一样,但没有X-XSRF-TOKEN被发送。

  methods: {
    onSubmit(e) {
      this.axios
        .post(
          e.target.action,
          { data: this.form },
          {
            withCredentials: true,
            xsrfCookieName: "XSRF-TOKEN",
            xsrfHeaderName: "X-XSRF-TOKEN"
          }
        )
        .then(res => {
          console.log(res)
        })
        .catch(err => {
          this.errors.push(err)
        })
    }
  }
Run Code Online (Sandbox Code Playgroud)

谢谢。

csrf axios x-xsrf-token

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

axios ×1

csrf ×1

x-xsrf-token ×1