【问题标题】:Axios not sending body [duplicate]Axios不发送正文[重复]
【发布时间】:2021-11-15 12:51:38
【问题描述】:

Axios 没有将正文发送到我的 API。

router.get('/login', async function(req,res) {
  try {
    const email  = req.body.email;
    const pass  = req.body.pass;
    const users = await pool.query("select username from users where username=$1 and password=$2", [email, pass]);
    if(users.rows[0])
      res.json(users.rows[0]['username']!=null);
    else
      res.json(users)
      console.log(req.body)
    console.log(email,pass)
  } catch (err) {
    console.log(err.message);
  }
});

当我在运行 GetData 后控制台记录 req.body 时

async function GetData() {

    const email = document.getElementById("email").value;
    const pass = document.getElementById("pass").value;
    const response = await axios.get("http://localhost:3002/login",
    {
      body: JSON.stringify({"email":  email, "pass" : pass})
    })
    console.log(response.config.body);
    console.log(response.data.rows)
  }

body 被称为 {} 一个空对象。

我如何通过 axios 发送身体,就像失眠发送身体一样。

当使用 insomnia 和完全相同的身体时,它应该返回 true。

【问题讨论】:

标签: javascript reactjs json axios get


【解决方案1】:

GET 不接受发送正文,请改用 POST:

router.post('/login', async function(req,res) {

// …

const response = await axios.post("http://localhost:3002/login", { email, pass })

另外,response.config.body 不是一个东西,如果你查看请求配置文档,有属性“data”,而不是“body”。

console.log(response.config.data);

希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 2019-08-29
    • 2020-08-24
    • 2019-03-04
    • 2019-10-16
    • 2021-02-11
    • 2021-11-29
    • 2021-11-13
    • 2018-03-06
    • 2021-01-03
    相关资源
    最近更新 更多