How to post query parameters with Axios?

前端 未结 2 1307
無奈伤痛
無奈伤痛 2020-12-04 18:47

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 :



        
相关标签:
2条回答
  • 2020-12-04 19:24

    In my case, the API responded with a CORS error. I instead formatted the query parameters into query string. It successfully posted data and also avoided the CORS issue.

            var data = {};
    
            const params = new URLSearchParams({
              contact: this.ContactPerson,
              phoneNumber: this.PhoneNumber,
              email: this.Email
            }).toString();
    
            const url =
              "https://test.com/api/UpdateProfile?" +
              params;
    
            axios
              .post(url, data, {
                headers: {
                  aaid: this.ID,
                  token: this.Token
                }
              })
              .then(res => {
                this.Info = JSON.parse(res.data);
              })
              .catch(err => {
                console.log(err);
              });
    
    0 讨论(0)
  • 2020-12-04 19:26

    axios signature for post is axios.post(url[, data[, config]]). So you want to send params object within the third argument:

    .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

    0 讨论(0)
提交回复
热议问题