微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当用户登录时,csrf 令牌不起作用 react js axios django

如何解决当用户登录时,csrf 令牌不起作用 react js axios django

当我退出并尝试发布它时,但当我登录并尝试发布时它不起作用并给我 403 错误

反应文件

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',headers:{
                    withCredentials: true,'X-CSrftoken':CSRF_TOKEN,'Accept': 'application/json','Content-Type': 'application/json',},})
        
      .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;

views.py

class create(CreateAPIView):

queryset = Post.objects.all()
serializer_class = postSerializers

也在setting.py中添加了这个

CSRF_COOKIE_NAME = "X-CSrftoken"

登录

enter image description here

退出

enter image description here

如果有足够聪明的人请帮助我解决这个问题......试图找到解决方案数天......

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。