如何在背景图片上设置授权标头:url()?

Mor*_*lst 2 javascript authorization http reactjs

我正在尝试通过REST API检索背景图像。

但是,这样做,我需要授权。

该令牌可从应该加载背景图像的上下文中获得,但是我不知道如何将其添加到请求中。

有任何想法吗?这有可能吗?

在另一种方法中,我使用Web服务器将授权添加到特定上下文中的所有请求。这工作正常,但不可能了。

Fab*_*ltz 5

一种方法是通过Javascript请求图像,设置正确的标题,然后将图像显示为对象URL / blob。这是一个例子:

fetch('https://i.imgur.com/PLKabDV.png', { headers: {
    "Content-Type": "application/json" // this header is just an example, put your token here
  } })
  .then(response => response.blob())
  .then(blob => {
    let img = document.getElementById('image');
    let url = URL.createObjectURL(blob);
    img.style.backgroundImage = `url(${url})`;
  })
Run Code Online (Sandbox Code Playgroud)
<div id="image" style="width: 430px; height: 430px;"></div>
Run Code Online (Sandbox Code Playgroud)