首页 > 解决方案 > 带有 Django/React 的 Axios 补丁:带有非表单的表单数据,不会更新数据库,邮递员工作正常

问题描述

我正在使用 Django 作为后端(REST API)和 React JS 作为前端的项目,使用 Axios 从我的 API 获取。

我尝试使用 imgs 和 JSON(表单数据以及非表单数据)修补数据。 通过选择 img 文件,它在 POSTMAN 中运行良好。但是在我的React 代码中,它不能更新 DB。会有什么问题?

非常感谢!

这是我的反应代码:

//init state
    const initialState = {
        location: "",
        avatar: null,
    };
 
 const [dbData, setDbdata] = useState(initialState);
--------
//upload img and setState
   let file = event.target.files[0];
   setDbdata({
     ...dbData,
     avatar: file,
   });

//Axios call
    axios.patch(
      `api/user/update/username=${state.username}`, dbData, {
           headers: {
             "content-type": "multipart/form-data;"
           },
         })
         .then((res) => {
           console.log(dbData.avatar)
         })
         .catch((err) => console.log(err.response));

在邮递员上工作正常

标签: javascriptdjangoreactjsaxiospostman

解决方案


推荐阅读