简体   繁体   English

Django + VueJS:POST 403 Forbidden - CSRF 令牌丢失或不正确

[英]Django + VueJS: POST 403 Forbidden - CSRF token missing or incorrect

I am running Django + Django REST framework in the backend and Vue.js in the frontend.我在后端运行 Django + Django REST 框架,在前端运行 Vue.js。 GET requests work fine, POST requests via Postman/Insomnia also do, but POST requests via the Vue.js frontend return an error in the Browser console: GET 请求工作正常,通过 Postman/Insomnia 的 POST 请求也可以,但通过 Vue.js 前端的 POST 请求在浏览器控制台中返回错误:

POST http://127.0.0.1:8000/api/questions/ 403 (Forbidden)
{detail: "CSRF Failed: CSRF token missing or incorrect."}

This is how I get the CSRF token and then fetch a POST request:这就是我获取 CSRF 令牌然后获取 POST 请求的方式:

File: csrf_token.js:文件:csrf_token.js:

import Cookies from "js-cookie";
var CSRF_TOKEN = Cookies.get("csrftoken");
export { CSRF_TOKEN };

File: api.service.js :文件: api.service.js

import CSRF_TOKEN from "./csrf_token.js";

async function getJSON(response) {
  if (response.status === 204) return "";
  return response.json();
}

function apiService(endpoint, method, data) {
  const config = {
    credentials: "same-origin",
    method: method || "GET",
    body: data !== undefined ? JSON.stringify(data) : null,
    headers: {
      "content-type": "application/json",
      "X-CSRFToken": CSRF_TOKEN
    }
  };
  return fetch(endpoint, config)
    .then(getJSON)
    .catch(error => console.log(error));
}

export { apiService };

MyComponent.vue:我的组件.vue:

...
methods: {
  onSubmit() {
    apiService(endpoint, method, { content: this.content })
      .then(response_content => {
        console.log(response_content)   
      });
  }
}
...

OK, in my case it was an export/import issue.好的,就我而言,这是一个导出/导入问题。

export default CSRF_TOKEN;

instead of代替

export { CSRF_TOKEN };

made the trick成功了

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Django 中的 ajax POST 期间禁止(CSRF 令牌丢失或不正确。) - Forbidden (CSRF token missing or incorrect.) during ajax POST in Django 尽管我在表单中有 csrf 令牌,但在 Django POST 请求中,禁止的 CSRF 令牌丢失或不正确 - Forbidden CSRF token missing or incorrect in Django POST request even though I have csrf token in form “禁止(CSRF 令牌丢失或不正确。):”使用 Django 和 JS - “Forbidden (CSRF token missing or incorrect.):” using Django and JS 禁止(CSRF 令牌丢失或不正确。) | Django 和 AJAX - Forbidden (CSRF token missing or incorrect.) | Django and AJAX CSRF令牌丢失或不正确的django - CSRF token missing or incorrect django CSRF令牌丢失或不正确(Django) - CSRF token missing or incorrect (Django) 尽管正确发送令牌,Django 服务器仍报告“禁止(CSRF 令牌丢失或不正确。)”? - Django server reporting "Forbidden (CSRF token missing or incorrect.)" despite sending token correctly? 用yui设置Django CSRF_TOKEN,但控制台显示“ django.request禁止(CSRF令牌丢失或不正确。)”。 - Set Django CSRF_TOKEN with yui, but console says 'django.request Forbidden (CSRF token missing or incorrect.)' 禁止(CSRF令牌丢失或不正确。)Django如何解决?用我的代码 - Forbidden (CSRF token missing or incorrect.) Django how to solve?WITH MY CODE 如何更正 django 中的以下代码,它给出错误“禁止(CSRF 令牌丢失或不正确。)” - How do I correct the following code in django, It's give an error “Forbidden (CSRF token missing or incorrect.)”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM