相关疑难解决方法(0)

CSRF InvalidAuthenticityToken 与 Rails 和 React

我无法让我的代码与 CSRF 令牌一起使用。

我有一个 axiosconfig 文件,我在其中设置 axios 并将其导出:

import axios from 'axios'

const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content')
const instance = axios.create({
  baseURL: 'http://api.domain.tld/v1/',
  headers: {
    'X-CSRF-Token': csrfToken
  }
});

export default instance
Run Code Online (Sandbox Code Playgroud)

以及我导入它的反应组件:

import axios from '../config/axios'
Run Code Online (Sandbox Code Playgroud)

在我提交的表单中,我解雇了这篇文章:

axios
  .post('/test', {
    longUrl: this.state.testValue
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
Run Code Online (Sandbox Code Playgroud)

我的头看起来像这样: 在此输入图像描述

axios 发布的请求是这样的: 在此输入图像描述

请求标头和我的头中的 CSRF 令牌是相同的,但我的 Rails-app 响应错误 422(不可处理的实体)并且:

ActionController::InvalidAuthenticityToken
Run Code Online (Sandbox Code Playgroud)

是否有可能是这个问题:

  // `xsrfCookieName` is the name of the cookie to use as a value for xsrf token …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails reactjs axios

5
推荐指数
0
解决办法
2500
查看次数

标签 统计

axios ×1

reactjs ×1

ruby-on-rails ×1