后台返回的base64格式的图片无法渲染到页面上

最近开发一个项目,需要后台返回一个二维码,所以后台返回给我一串base64格式的字符串。本以为直接img标签渲染即可,但死活不出来,以为我的方法有问题,一直在查找。后来发现是后台返回的base64图片格式的字符串 带有换行和空格,写了一个方法去掉换行和空格,图片立马渲染成功(吐槽一下。。)

 getQRcode () {
            let _this = this
            this.$http.get('/api/xxxxxxxxxxxxx/qrcode', {}).then((res) => {
                if (res) {
                    // 后台返回的base64图片格式去掉空格和换行
                    let str = res.replace(/\. +/g, '')
                    str = str.replace(/[\r\n]/g, '')
                    _this.qrcode = str
                } else {
                    console.log('图片不存在')
                }
            }).catch(err => {
                console.log(err.status, err.message)
            })
        }

猜你喜欢

转载自blog.csdn.net/qq_35430000/article/details/84852207