post 在 Axios 中使用 400 或 401 请求 - 不记名令牌

post request in Axios with 400 or 401 - bearer token

我正在尝试使用 axios post 数据。

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 - eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZGNmZjFhMjQzZWUwNTJmNmNkMjAzYzgiLCJpYXQiOjE1NzQ1ODQzMDl9.P9cKtljoFXbCo6X540tQ27d_jeouOwwRJ6HKhpsy-PY asdad

App.js:100 login clicked

App.js:108 {"title":"asdad","text":"aaaaaaaaaa","category":"","creationDate":"2019-10-0, 9:31","done":false} :3000/tasks/:1 POST http://localhost:3000/tasks/ 400 (Bad Request)

在 devtools 中请求详细信息:

您需要像这样 JSON.stringify 发送数据:

let data = {
  title,
  text,
  category,
  creationDate,
  done: false
};

此外,您的 creationData 无效,必须采用以下格式:

"2019-11-24T09:06:29.271Z"