SoFunction
Updated on 2025-04-08

Solve the problem that the axios post backend cannot receive data

When the backend allows cross-domain, axios post passes data to the backend, but the backend cannot receive it and is always empty

The request can reach the backend normally, so there will definitely be a problem with the place where the data is transmitted.

So here the headers are set to

headers:{"Content-Type":'application/x-www-form-urlencoded;charset=UTF-8'}

In addition transformRequest function

transformRequest: [function (data) {
                // Perform arbitrary conversion of data                let ret = '';
                for (let it in data){
                  ret += encodeURIComponent(it)+'='+encodeURIComponent(data[it])+'&'
                }
                return ret

                // return this.$(data);
              }],

The following are all the codes

this.$('http://127.0.0.1:8123/addapp',{name:"test",desc:"test"},{

  transformRequest: [function (data) {
     // Perform arbitrary conversion of data     let ret = '';
                for (let it in data){
                  ret += encodeURIComponent(it)+'='+encodeURIComponent(data[it])+'&'
                }
                return ret

                // return this.$(data);
              }],
              headers:{
                  "Content-Type":'application/x-www-form-urlencoded;charset=UTF-8'
              }
            })
              .then(function (response) {
                (response);
              })
              .catch(function (error) {
                (error);
              });

          } else {
            this.$('Fail!');
          }
        })

The above article solves the problem that the axios post backend cannot receive data is all the content I have shared with you. I hope it can give you a reference and I hope you can support me more.