获取 api 和 csrf 令牌 Rails 后端

问题描述 投票:0回答:1

我有一个带有设备验证功能的 Rails 后端。

我正在尝试使用 fetch api,但失败并显示错误消息

can't verify CSRF token

这是获取代码,我真的找不到另一个角落来寻找问题,你能帮忙吗?

body = JSON.stringify({
  email:'[email protected]', 
  password: 'abcdef', 
})
headers= {
        'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'X-Requested-With': 'XMLHttpRequest'
      }
method = 'post'


fetch('/users/sign_in',{method,body, headers, credentials: 'same-origin' })

谢谢你!

ruby-on-rails devise fetch-api
1个回答
-2
投票

我认为您在

fetch
函数中的参数是错误的。它应该是这样的:

fetch('/users/sign_in',{ method: method, body: body, headers: headers, credentials: 'same-origin' })

© www.soinside.com 2019 - 2024. All rights reserved.