axios请求图片乱码

  • 由于axios的默认responseType是:application/json, text/plain,会导致请求的图片显示乱码
  • 指定 responseType: ‘blob’即可

    1. function createImg(){
    2. const that = this
    3. const baseUrl = 'http://xxx.com/'
    4. axios({
    5. method: 'get',
    6. url: baseUrl + "/app/wx/getqrCode", // 请求地址
    7. data: {} , // 参数
    8. responseType: 'blob' // 表明返回服务器返回的数据类型
    9. }).then((res) => {
    10. let data = window.URL.createObjectURL(res.data)
    11. if (data) {
    12. that.setState({
    13. url: data
    14. })
    15. wxShare({
    16. // 图片需是绝对路径
    17. img: require('../../static/img/mine/qrcode.png').replace(/\.\.\//, baseUrl),
    18. url: `${baseUrl}/haibao.html`,
    19. desc: '瑜伽界',
    20. title: '分享领红包',
    21. })
    22. }
    23. })
    24. }

    分享缩略图无效

    • 图片域名需要和公众号对应的js安全域相同
    • 返回的图片不能使用base64
    • 必须使用绝对路径