reactjs - axios在strapi.io中提出编辑用户的请求不起作用
问题描述
我正在尝试使用 Strapi.io 中的 axios PUT 更新用户头像。我最初是在 componentDidmount 上获取用户数据。
初始状态
first_name: "Foo"
last_name: "Bar"
address: "456 address"
city: "City Address"
country: "Philippines"
zipcode: "1234"
error: ""
avatar: "https://limitless-brushlands-81295.herokuapp.com/uploads/ed4648185e1e425c884beabb99a92695.png"
这是用户提交编辑数据时的代码
handleOnSubmit = (event) => {
const {first_name,last_name,address,city,country,zipcode,avatar} = this.state
event.preventDefault();
const data = {
first_name,
last_name,
address,
city,
country,
zipcode
};
console.log(data)
var bodyFormData = new FormData();
bodyFormData.append('files.image', avatar)
bodyFormData.append('data', JSON.stringify(data));
axios({
method: 'PUT',
url: `${strapi}/users/${getId()}`,
data: bodyFormData,
headers: {
'Content-Type': 'multipart/form-data',
}
})
.then(function (response) {
// window.location.href = `/profile/${getId()}`;
console.log(response.data);
})
.catch(function (response) {
//handle error
console.log(response);
});
}
我收到 200 条回复,但不会更新。我检查了控制台的状态,这是输出
first_name: "Foobar"
last_name: "Bar"
address: "123 address"
city: "City Address"
country: "Philippines"
zipcode: "4026"
error: ""
avatar: "blob:http://localhost:3000/ecf9e21f-1081-4afc-80b1-1223cb6058bd"
此日志是在用户输入数据之后
我希望有一个人可以帮助我
这是场景
使用 axios 获取来自strapi 的数据获取请求
first_name: "Foo"
last_name: "Bar"
address: "456 address"
city: "City Address"
country: "Philippines"
zipcode: "1234"
error: ""
avatar: "https://limitless-brushlands-81295.herokuapp.com/uploads/ed4648185e1e425c884beabb99a92695.png"
应该用这个数据代替
first_name: "Foobar"
last_name: "Bar"
address: "123 address"
city: "City Address"
country: "Philippines"
zipcode: "4026"
error: ""
avatar: "blob:http://localhost:3000/ecf9e21f-1081-4afc-80b1-1223cb6058bd"
解决方案
尝试使用下面的axios put方法并检查是否有响应。请提及什么未更新,状态更新或您的回复未来?
const body = {body : bodyFormData};
const header = {
headers: {
'Content-Type': 'multipart/form-data',
}
}
axios.put(`${strapi}/users/${getId()}` ,body ,header)
.then(res => {
console.log(res)
})
在 axios 中,第一个参数是 URL,第二个参数是 header。但是对于 POST 或 PUT,第二个参数充当正文,第三个参数充当标题
推荐阅读
- sequelize.js - Sequelize - 如何选择 COUNT(*) 并进行 GROUP BY?
- python - 如何使用 tkinter 正确显示 numpy int16 数组?
- netsuite - NetSuite SuiteScript 2.0 如何从 UserEvent 按钮创建银行存款单(suitelet 功能)
- excel - vArray 值未从 VBA 中的上一个循环中清除
- python - 如何防止 Keras 顺序模型中的过度拟合?
- angular - Nativescript gradle 构建失败并显示“找不到资源 xml/network_security_config”
- spring - 为什么 Spring Boot 4“自定义错误页面”失败 404?
- reporting-services - SSRS 报告垂直合并单元格
- c - 访问动态分配的 C 结构数组时出现段错误
- kubernetes - kubeadm init kubelet 抱怨默认绑定地址已在使用中