vue获取图片验证码

 
        
            
              
              
            
          
           
          
            
                 
            
          
        

所有请求采用axios进行封装;

//获取图片校验码
    refreshCaptcha: function(){
      this.loginForm.timeStamp=new Date().getTime();
      let params={
        "data":{
        "timeStamp":this.loginForm.timeStamp
        }
      }
      this.$api.LoginManage.getPhotoValidCode(params).then((res)=>{
        let img = btoa(
          new Uint8Array(res.data).reduce((data, byte) => data + String.fromCharCode(byte), '')
        );
        this.loginForm.src=  'data:image/png;base64,' +img;
      })
    },

后端返回为图片流,所以要加 responseType: "arraybuffer" 这句代码;

// 获取图片验证码
export const getPhotoValidCode = (params) => {
    return axios({
        url: 'loginInfo/getPhotoValidCode',
        responseType: "arraybuffer",
        method: 'post',
        params
    })
}

你可能感兴趣的:(vue获取图片验证码)