【发布时间】:2021-03-09 15:49:36
【问题描述】:
我试图发出 post 请求并遇到错误代码 403,csrf 令牌丢失.. 尝试了很多方法,但最后没有在 settings.py 上禁用 csrf 令牌并工作,所以发现问题出在 csrf 令牌中
class Axios extends React.Component{
constructor(){
super()
this.state = {
persons: []
}
}
post(){
axios.post('http://127.0.0.1:8000/api/create', {
title: 'titan',
body: 'this is working',
})
.then(function (response) {
console.log(response);
})
}
get(){
axios.get('http://127.0.0.1:8000/api').then(Response=>{
console.log(Response.data)
})
}
componentDidMount(){
this.post();
this.get();
}
render(){
return(
<div>
<h1>fetch</h1>
</div>
)
}
}
export default Axios;
这是我的代码,请告诉我一种将 csrf 令牌放入其中的方法以及导入语句...
目前正在使用 axios 谢谢
【问题讨论】:
-
使用 axios,而不是 ajax..
-
Axios 做 AJAX。这里专门针对axiosstackoverflow.com/questions/39254562/…请使用搜索。
标签: reactjs django django-rest-framework axios