iframe或者window.open()添加请求头方法

  1. 首先我们要明白iframe是不能添加请求头的,这里我们能做的就是改造iframe 里的src指向页面,通过异步请求,添加请求头,拿到html数据,再反写进iframe
    例如:
    在当前vue页面修改iframe
     
    <iframe id="report_id" src="./query.html" style="width: 100%;height: 450px;"  frameborder="0">
                    </iframe>
    //初始化方法
    init(){
               //发送请求得到html数据
            this.$http.get(this.$route.query.url).then(res =>{
                    this.form=res;
                    var iframeWin = this.$refs['report_id'].contentWindow;
                       //向子页面发送参数
                    iframeWin.postMessage({
                        url: this.$route.query.url,
                        data:res //页面html代码res
                    }, '*');
                });
    }

    在子页面query.html里面拿到父页面vue页面的参数data,将data里的html渲染进子页面的iframe

    <iframe id="app" name="app"  style="width: 100%;height: 650px;"  frameborder="0">
                
      <script type="text/javascript">
        window.URL="";
        window.onload=function(){
                //通过监听事件接收参数
            window.addEventListener("message", function(e){
               var iframe = window.frames['app'];
                //打开iframe.document
                iframe.document.open();
                //iframe.document写入html数据
                iframe.document.write(e.data.data);
                //iframe.document关闭
                iframe.document.close();
            });
        }
    
    </script>

  2. window.open也是不能添加请求头的,
    如果是跳转页面,可以像上面方法那样,通过iframe内塞入html页面代码;
    如果是打印或者导出,可以通过iFrame.window.print()和XMLHttpRequest来实现.
    const iframe=window.frames['_print_pdf_frame'];  
    
    $("iframe[name='_print_pdf_frame']").on("load",function(){
                    iframe.window.focus();
                    iframe.window.print();
    });
            
    iframe.window.focus();
    iframe.location.href=url;//赋值iframe的路径
    //导出pdf按钮事件
    $(`.export-pdf`).click(function(){
            const urlParameters=buildLocationSearchParameters();
            const url=window._server+'/pdf'+urlParameters;
            windowOpen(url,'.pdf');
    
        });
    
    function windowOpen(url,typeName){
        var xhr = new XMLHttpRequest();
        var fileName = window.fileName + typeName; // 文件名称
        xhr.open('GET', url, true);
        xhr.responseType = 'blob';
        xhr.setRequestHeader('token',window.token);
        xhr.onload = function(res) {
            if (this.status === 200) {
                var type = xhr.getResponseHeader('Content-Type');
                var blob = new Blob([this.response], {type: type});
                if (typeof window.navigator.msSaveBlob !== 'undefined') {
                    /*
                     * For IE
                     * >=IE10
                     */
                    window.navigator.msSaveBlob(blob, fileName);
                } else {
                    /*
                     * For Non-IE (chrome, firefox)
                     */
                    var URL = window.URL || window.webkitURL;
                    var objectUrl = URL.createObjectURL(blob);
                    if (fileName) {
                        var a = document.createElement('a');
                        if (typeof a.download === 'undefined') {
                            window.location = objectUrl;
                        } else {
                            a.href = objectUrl;
                            a.download = fileName;
                            document.body.appendChild(a);
                            a.click();
                            a.remove();
                        }
                    } else {
                        window.location = objectUrl;
                    }
                }
            }
        }
        xhr.send();
    };

    相关资料参考:
    XMLHttpRequest 对象
    XMLHttpRequest 及其open()的用法_小木木的开发日常-CSDN博客

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据提供的引用资料,当使用window.open打开一个新窗口时,有时会遇到404错误。这个问题可能与浏览器的安全机制有关,浏览器会将用户在页面上的操作视为非法操作,并在一段时间内屏蔽window.open。根据引用中提供的代码,可以封装一个函数来处理这个问题。该函数会在用户操作页面4.5秒后,如果接口请求没有完成,就先执行window.open,然后在接口请求完成后替换新窗口的URL来完成文件下载。 然而,如果在4.5秒内接口请求已经完成,函数会直接执行window.open。这样可以避免window.open被屏蔽的问题。请注意,这个函数是通过设置一个定时器来控制执行的时机。 关于nginx和window.open的404错误,引用中提到了一个解决方法,即使用Cookie来存储token并在新打开的页面中获取。这种方法需要在同一个域名下使用Cookie,如果出现跨域情况,则需要通过配置代理转发来解决。 另外,引用中也提到了另一种方法,即在跳转链接后面拼接token,然后在新打开的页面中从链接中获取token并存储在请求头部。这种方法直接在URL后面拼接参数,不会出现跨域情况。但是需要注意的是,这种方法只能在页面中获取token,无法在请求拦截器中获取。 综上所述,要解决nginx和window.open的404错误,可以尝试使用上述两种方法之一来处理token的传递。如果可以在同一个域名下使用Cookie,则可以选择使用Cookie来存储和获取token。如果出现跨域情况,则可以尝试在URL后面拼接token的方式来传递token。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [window.open无法打开新窗口](https://blog.csdn.net/a986436517/article/details/118631124)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [iframe/window.open/a三种标签打开新页面或新窗口设置请求头;实现免密登录](https://blog.csdn.net/weixin_42342065/article/details/127420783)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dmlcq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值