简体   繁体   English

在Axios中以400或401发布请求-承载令牌

[英]post request in Axios with 400 or 401 - bearer token

I'm trying to post data using axios. 我正在尝试使用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"
    }
  });
};

Now I have 400 error, in some options I got Unauthorized error. 现在我有400错误,在某些情况下,我出现了未授权错误。 I tried many times, but i failed. 我尝试了很多次,但是失败了。

Console: 安慰:

Apptask - eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZGNmZjFhMjQzZWUwNTJmNmNkMjAzYzgiLCJpYXQiOjE1NzQ1ODQzMDl9.P9cKtljoFXbCo6X540tQ27d_jeouOwwRJ6HKhpsy-PY asdad Apptask-eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZGNmZjFhMjQzZWUwNTJmNmNmNkMjAzYzgiLCJpYXQiOjE1NzQ1ODQzYxYdWyKdY6YPJCyJdWyKyxYdJKyTyJdWyKdJ6YPjCyJdWyDjCyJdWyDjCyJdWyDjCyJdWyDJJWyZDjCyJdWyDJJWJDJWJWJwjDjCyTjCyJdWyDJWJWJYJwjDjCyJDJWJWJWJWJYJWJWJWJWJYJWJWJWJWJWJW}

App.js:100 login clicked App.js:点击了100次登录

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) 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(错误请求)

Request details in devtools: 在devtools中请求详细信息:

在此处输入图片说明

You need to send data without JSON.stringify like this: 您需要像这样不使用JSON.stringify发送数据:

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

Also your creationData is not valid, it must be in this format: 同样,您的creationData无效,它必须采用以下格式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM