首页 > 技术文章 > axios之post提交

sunny-yu 2019-09-28 20:37 原文

axios官网地址:https://github.com/axios/axios

post提交到后台需要做相对应的处理

使用URLSearchParams可以让post 数据提交到后台

对应gitHub上的内容如下:

       In a browser, you can use the URLSearchParams API as follows:

       const params = new URLSearchParams();
        params.append('param1', 'value1');
       params.append('param2', 'value2');
      axios.post('/foo', params);


     post提交之头像上传:

      首先先阅读gitHub上axios关于头像上传的介绍

      查看文件examples里面的upload  里面相对应有处理介绍

        首先这有一个demo是简单的图片上传

         <img :src="headImg" alt="">
    <input type="file" id = "head" @change="uploadImg()">

      // 图片上传
 uploadImg(){
  //  console.log(document.getElementById('head').files[0])  点取消输出undefined

  if(document.getElementById('head').files[0]){
         var data = new FormData();
          data.append('headfile', document.getElementById('head').files[0]);
          axios.post("输入上传接口地址",data).then(rest=>{
          console.log(rest);
          if(rest.data.code===200){
            this.headImg = rest.data.data.msbox;
          }
   })
  }
   
 }

推荐阅读