Javascript 如何使用 Axios 发布查询参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/53501185/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to post query parameters with Axios?
提问by GuillaumeRZ
I am trying to post on an API with some query params. This is working on PostMan / Insomnia when I am trying to by passing mail and firstname as query parameters :
我正在尝试使用一些查询参数在 API 上发布。当我尝试通过将邮件和名字作为查询参数传递时,这适用于 PostMan/Insomnia:
 http://localhost:8000/api/mails/users/sendVerificationMail?mail=lol%40lol.com&firstname=myFirstName
However, when I am trying to do it with my react native app, I got a 400 error (Invalid Query Parameters).
但是,当我尝试使用 React Native 应用程序执行此操作时,出现 400 错误(查询参数无效)。
This is the post method :
这是post方法:
.post(`/mails/users/sendVerificationMail`, {
  mail,
  firstname
})
.then(response => response.status)
.catch(err => console.warn(err));
(my mail and firstname are console.logged as follow: [email protected]and myFirstName).
(我的邮件和名字是 console.logged 如下:[email protected]和myFirstName)。
So I don't know how to pass Query Parameters with Axios in my request (because right now, it's passing data: { mail: "[email protected]", firstname: "myFirstName" }.
所以我不知道如何在我的请求中使用 Axios 传递查询参数(因为现在,它正在传递data: { mail: "[email protected]", firstname: "myFirstName" }.
回答by enapupe
axios signature for post is axios.post(url[, data[, config]]). So you want to send params object within the third argument:
帖子的 axios 签名是axios.post(url[, data[, config]]). 所以你想在第三个参数中发送 params 对象:
.post(`/mails/users/sendVerificationMail`, null, { params: {
  mail,
  firstname
}})
.then(response => response.status)
.catch(err => console.warn(err));
This will POST an empty body with the two query params:
这将发布一个带有两个查询参数的空主体:
POST http://localhost:8000/api/mails/users/sendVerificationMail?mail=lol%40lol.com&firstname=myFirstName
POST http://localhost:8000/api/mails/users/sendVerificationMail?mail=lol%40lol.com&firstname=myFirstName

