首页 > 解决方案 > 服务器响应状态为 415(不支持的媒体类型) - vue.js 和 python django

问题描述

我正在发送注册请求,并收到此错误:

{"errors":[{"detail":"请求中不支持的媒体类型 \"application/json;charset=UTF-8\"。","source":{"pointer":"/data"},"status ":"415"}]}

这就是我在 vue.js 中发送数据的方式:

import {instanceAxios} from '@/services/interceptor';
const registration = async ({commit}, data) => {
    try {
        let resp = await instanceAxios.post('/account/register/', data);
        const token = resp.data.token;
        localStorage.setItem('token', token);
        instanceAxios.defaults.headers.common['Authorization'] = `Token ${token}`;
        commit('AUTH_SUCCESS', resp.data);
        return resp;
    } catch (e) {
        localStorage.removeItem('token');
        throw e;
    }
};

我得到这个错误。

这是我的请求在浏览器中的外观:

在此处输入图像描述 内容类型是应用程序的非常有趣的事情我也可以从后端注册方法中为您提供代码 https://pastebin.com/74zXjejV

PS:我不知道为什么,但我得到内容类型 application/d.json - 这很奇怪

标签: djangovue.js

解决方案


415 错误意味着您的有效负载无效,请参见此处

如果你发送 json 试试这个

instanceAxios({
      method: 'post',
      url: '/account/register/',
      headers: {'Content-Type': 'application/json'}
    });

代替

instanceAxios.post('/account/register/', data);

并检查您的身体是否有效


推荐阅读