axios在vue中的应用(二)—— 表单提交上传图片

使用axios实现图片预览、图片上传等功能:

methods: {
    // 图片预览
    priviewImg(e) {
        // ...
    },
    // 提交表单
    sumitRefund() {
        let fd = new FormData();
        fd.append("reason", this.refundReason);
        fd.append("money", this.refundMoney);
        fd.append("order", this.refundOrder);
        fd.append("pic", this.sendImg);
        // 设置请求头
        let config = {
             headers: {
                'Content-Type': 'multipart/form-data'  //以表单传数据的格式来传递data
            }
        };
       this.$http.post("service/refund", fd, config)
          .then(res => {
              console.log(res);
          })
          .catch(err => {
              console.log(err);
          });
      }
  }

猜你喜欢

转载自blog.csdn.net/xiaomajia029/article/details/82909964