如何使用 axios 发送查询字符串中的数字

问题描述 投票:0回答:1

我的 React 应用程序调用 API 来发布查询字符串。

例如,

var param = {

  name: 'Alex',
  age: 22
}

axios({
    baseURL: 'https://aaa.com',
    url: '/user/create', 
    method: 'post', 
    params: param,
    headers: {
      Authorization: `Bearer ${_token}`
    },
})

这个效果很好。但是,年龄的值是作为字符串插入的。就像这个“22”。 参数中的所有数字都作为字符串插入。

我尝试使用“qs”。

axios.defaults.paramsSerializer = params => {

    var paramStr = qs.stringify(params)

    return paramStr
}

但结果是一样的。

我需要对查询字符串使用其他编码技术吗?

javascript axios encoding parameters numbers
1个回答
0
投票

您可以使用下面的代码使用 axios 高效地将数据发送到服务器:

const { data } = await axios.post( 
        `${server}/users/login`,
        {
          // Put your all params here with query
        },
        {
          headers: {
            "Content-Type": "application/json",
            "Authorization": `Bearer ${_token}`
          },
          withCredentials: true,
        }
      ).then( (response) => {
        console.log(response);
      })
      .catch( (error) => {
        console.log("Axios Error: " + error.message);
      });
© www.soinside.com 2019 - 2024. All rights reserved.