How to post query parameters with Axios?

前端 未结 2 1306
無奈伤痛
無奈伤痛 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);
              });
    

提交回复
热议问题