袁来如此的工作笔记
袁来如此的工作笔记
竹杖芒鞋轻胜马,谁怕? 一蓑烟雨任平生。

VUE+iframe添加请求头

浏览量:2

<iframe id="iframe" src="" />

setTimeout(() => {
  var iframe = document.querySelector("#iframe");
  this.populateIframe(iframe, [["Authorization", "Bearer " + getToken()]]);
}, 0);

  methods: {
    populateIframe(iframe, headers) {
      var xhr = new XMLHttpRequest();
      xhr.open("GET", 'http:localhost:8080/xxx');
      xhr.responseType = "blob";
      headers.forEach((header) => {
        xhr.setRequestHeader(header[0], header[1]);
      });
      xhr.onreadystatechange = () => {
        if (xhr.readyState === xhr.DONE) {
          if (xhr.status === 200) {
            iframe.src = URL.createObjectURL(xhr.response);
          }
        }
      };
      xhr.send();
    },
  }

打赏