解决用axios发送请求,后台接收不到请求数据的问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39403545/article/details/82116780

在<script></script>里增加以下代码

var HTTP = axios.create({
      baseURL:'http://localhost:8081/', //这是基础url
      headers: {'Content-Type': 'application/x-www-form-urlencoded'},
      transformRequest: [function (data) {
        // Do whatever you want to transform the data
        let ret = ''
        for (let it in data) {
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        return ret
      }]
    });

使用:axios.post改为HTTP.post,例如

HTTP.post('admin/user/login.action',{
    name:'test',password:111
})
.then(function(response){
    console.log(response.data);
});

备注 :其实就是把请求的数据格式化

猜你喜欢

转载自blog.csdn.net/qq_39403545/article/details/82116780