下载 post请求 携带token

downOut () {
		//获取地址
      const env = process.env.VUE_APP_ENV
      let url = config.baseUrl[env] + "Device/ExportDevice"
      //创建请求
      const req = new XMLHttpRequest();
      //请求体 JSON格式   其他格式参考:https://blog.csdn.net/harryhare/article/details/80778066
      let formData = JSON.stringify(this.selectedRowKeys);
      req.open('POST', url, true);
      req.responseType = 'blob';
      req.setRequestHeader('Content-Type', 'application/json');
      //请求携带token
      req.setRequestHeader('token', localStorage.getItem('sysToken'))
      req.onload = () => {
        const data = req.response;
        const a = document.createElement('a');
        const blob = new Blob([data], { type: "application/octet-stream" });
        const blobUrl = window.URL.createObjectURL(blob);
        this.download(blobUrl);
      };
      req.send(formData);
    },
    download (blobUrl) {
      const a = document.createElement('a');
      a.style.display = 'none';
      a.download = 'xxx.xls'; //文件名
      a.href = blobUrl;
      a.click();
      // document.body.removeChild(a);
    },
上一篇:DOM对象


下一篇:使用 JavaScript 调用 Web API