我正在尝试使用axios发布数据。
handleAddClickAxios = (token, title, text, category, creationDate) => {
const api = "http://localhost:3000/tasks/";
console.log("login clicked");
let data = JSON.stringify({
title: title,
text: text,
category: category,
creationDate: creationDate,
done: false
});
console.log(data);
axios.post(api, data, {
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json"
}
});
};
现在我有400错误,在某些情况下,我出现了未经授权的错误。我尝试了很多次,但是失败了。
控制台:
Apptask-。阿斯达德
App.js:100登录已单击
App.js:108{“ title”:“ asdad”,“ text”:“ aaaaaaaaaa”,“ category”:“”,“ creationDate”:“ 2019-10-0,9:31“,”完成“:false}:3000 / tasks /:1 POSThttp://localhost:3000/tasks/400(错误请求)
在devtools中请求详细信息:
您需要像这样不使用JSON.stringify发送数据:
let data = {
title,
text,
category,
creationDate,
done: false
};
也您的creationData无效,它必须采用以下格式:
“ 2019-11-24T09:06:29.271Z”