Django Rest Framework缺少CSRF令牌Vue

时间:2019-01-07 17:36:35

标签: django api vue.js django-rest-framework

好吧,我有我的代码,几天前它运行良好,但是现在当我尝试使用两种方法时,它说“ CSRF失败:CSRF令牌丢失或不正确”。这是我的代码:

/* Logout Function I'm using rest_auth */ 
   function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie !== '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);

                if (cookie.substring(0, name.length + 1) === (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
    var csrftoken = getCookie('csrftoken');     
    const router = new VueRouter({
        routes: [
            {path: '/logout', alias: '/logout'}
        ],
    });
    new Vue({
        el: '#logout',
        router: router,
        delimiters: ['${','}'],
        headers: {'HTTP_X_XSRF_TOKEN': csrftoken},
        data:{      
            loading: false,
            newUser: { 'username': null, 'password': null},
        },
        mounted: function(){

        },
        methods: {
            logout: function(){
                this.$http.post('/auth/logout/').then((response) => {               
                    loading = true;
                    sessionStorage.removeItem("auth");
                    router.go('/login');
                }).catch((err) => {
                    console.log(err);
                })
            }
        }
    });    

这几天前没问题。请帮我!并谢谢你!

0 个答案:

没有答案