禁止:/api/v1/token/logout/ "POST /api/v1/token/logout/ HTTP/1.1" 403 58

问题描述 投票:0回答:1

我在学习教程后遇到此问题,并且无法识别“MyAccountView.vue”页面中的错误。我尝试更改为 re_path 但没有成功。

Forbidden: /api/v1/token/logout/
[16/Oct/2023 19:01:35] "POST /api/v1/token/logout/ HTTP/1.1" 403 58

代码:

URLS.PY

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/v1/', include('djoser.urls')),
    path('api/v1/', include('djoser.urls.authtoken'))
]

设置.PY

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSESS':(
        'rest_framework.authentication.TokenAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSESS':(
        'rest_framework.permissions.IsAuthenticated',
    )
}

错误图像

Getting this error in browser's console

MyAccountView.vue

如果有效,我应该转发到“/”我网站的主页

methods: {
        logout() {
            axios
                .post("/api/v1/token/logout/")
                .then(response => {
                    axios.defaults.headers.common["Authorization"] = ""

                    localStorage.removeItem("token")

                    this.$store.commit('removeToken')

                    this.$router.push('/')
                })
                .catch(error => {
                    if (error.response) {
                        console.log(JSON.stringify(error.response.data))
                    } else if (error.message) {
                        console.log(JSON.stringify(error.message))
                    } else {
                        console.log(JSON.stringify(error))
                    }
                })
        }
    }
python html node.js django vue-cli-3
1个回答
0
投票

看起来您正在使用

djoser

您需要在 Post 请求中调用注销端点时传递令牌

示例:

.post('api/v1/token/logout/', token,
   {
     headers: {
       Authorization: `Token ${token}`  --> Add your token here.
     }
   })
© www.soinside.com 2019 - 2024. All rights reserved.