[英]Axios.post in React 422, Error: (Data Validation Failed.)
我有一個想要通過post
方法發送到數組的item
對象。 我收到錯誤消息: POST https://applic.com/api/v1/todos?expand=createdBy 422 (Data Validation Failed.)
我的物品對象:
creat: Sat Jun 01 2019 00:15:00 GMT+0200 (Central European summer time) {}
desc: "bbbb"
id: "123456-9bbc-4f85-1234-fdfdfdfdfdfdds"
price: 900
stat: "50"
parent_id: "12345678-123r-45frt6-b6678-12345567"
api中的示例對象:
creat: "2019-06-28 12:58:02+00"
desc: null
id: "c123545-12sd-67ui-w234-5ghg789"
pend: false
price: 60
stat: 50
parent_id: "12345678-123r-45frt6-b6678-12345567"
updat_by: null
我糾正了:我的對象-> stat: parseInt(50)
屬性creat
-> toISOString
>返回我"2019-06-29T12:45:53.594Z"
api返回我"2019-06-28 12:58:02+00"
是問題2019-06-29T12:45:53 .594Z
2019-06-28 12:58:02 +00
?
我必須按要求發送正文
碼:
const url = `https://applic.com/api/v1/todos?expand=createdBy`;
const token = '12345';
add = (item) => {
axios.post(
url,
{
data: item
},
{
headers: { 'Authorization' : `Bearer ${token}`}
}).then( res => {
console.log(res.data);
}).catch( err => {
console.log(err);
});
let newArray = [...this.state.todos];
newArray.push(item);
this.setState({
todos: newArray
});
}
這個怎么樣 ?
const url = `https://applic.com/api/v1/todos?expand=createdBy`;
const token = '12345';
add = (item) => {
axios({
method: 'POST',
url,
data: item,
headers: { Authorization: `Bearer ${token}` },
})
.then(res => {
console.log(res.data);
}).catch(err => {
console.log(err);
});
this.setState({
todos: [...this.state.todos, item],
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.