Img src path with header params to pass

前端 未结 4 727
攒了一身酷
攒了一身酷 2020-12-03 02:07

I have an img tag in jsp page where the src path requires header parameters to pass to get the image. How can we achieve it?

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 02:18

    You can now use fetch() to add your headers and then load the result into an :

    const src = 'https://api.mywebsite.com/profiles/123/avatar';
    const options = {
      headers: {
        'Some-Header': '...'
      }
    };
    
    fetch(src, options)
    .then(res => res.blob())
    .then(blob => {
      imgElement.src = URL.createObjectURL(blob);
    });
    

提交回复
热议问题