我有一个带有设备验证功能的 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' })
谢谢你!
我认为您在
fetch
函数中的参数是错误的。它应该是这样的:
fetch('/users/sign_in',{ method: method, body: body, headers: headers, credentials: 'same-origin' })