我无法让我的代码与 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)
请求标头和我的头中的 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)