下载文件时,接口请求返回内容一般有两种: 直接返回文件内容,返回一个url

用window.open()直接打开请求地址或者返回的url,可能会因为跨域问题导致浏览器拦截

解决办法是:在请求,打开一个窗口,然后将请求地址或者返回的url直接赋值给该窗口的href

1. 直接返回文件内容: 打开请求api地址

 downPkg() {
      let mywin = window.open('','_self'); 
    mywin.location.href = apiPath;
 }

2. 返回url时:打开返回的url

    downPkg() {
      var mywin = window.open('','_self');  //注意一定要在请求前操作此步
      this.$server.getPackage_async().then(res => {  //这里是自己封装的ajax方法
        if (res.result) {
          mywin.location.href = res.data;
        }
      });
    }

 

相关文章:

  • 2021-11-21
  • 2021-12-31
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-31
  • 2022-12-23
  • 2022-02-13
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-28
  • 2022-12-23
  • 2022-03-11
  • 2022-12-23
  • 2021-12-10
相关资源
相似解决方案