zoukankan      html  css  js  c++  java
  • 对iframe里的fetch和xmlhttprequest拦截并对request/reponse进行包装

    写了一个通常的函数可以对iframe里的fetch和xmlhttprequest拦截并对request/reponse进行包装

    rewriteFetchandXmlhttp(iframeWin) {
            if (!iframeWin) {
                console.error("iframe handle was lost");
                return;
            }
            const token=sessionStorage.getItem("uic-token") || '';
            if(!token || token=='undefined'){
                return;
            }
            // const iframeWin=iframeWin;
            const _fetch=iframeWin.fetch;
            iframeWin.fetch = function (url, options = {}) {
               // console.log("fecth url->:",url);
                //console.log("fecth url->:",{options});
                if (options.headers) {
                    options.headers['uic-token'] = token;
                } else {
                    options.headers = { "uic-token": token }
                }
                return new Promise((resolve, reject) => {
                    _fetch(url, options, resolve, reject).then((res) => { resolve(res) }).catch((err) => { reject(err) });
                });
            };
    
            const send = iframeWin.XMLHttpRequest.prototype.send;
            iframeWin.XMLHttpRequest.prototype.send = function (data) {
              // console.log("add header token:",token);
              this.setRequestHeader("uic-token", token);
              send.call(this, data);
            };
            //iframeWin.XMLHttpRequest = XMLHttpRequest;
            return;
    
    
            // //console.log("XMLHttpRequest开始:");
            // const xmlreqc = iframeWin.XMLHttpRequest;
            // const XMLHttpRequest = function () {
            //     this.xhr = new xmlreqc();
            //     return this;
            // };
            // //var xhr = new window.XMLHttpRequest();
    
            // XMLHttpRequest.prototype.open = function (method, url, async) {
            //     //console.log("open: XMLHttpRequest url->:" + url);
            //     return this.xhr.open(method, url, async); //send it on
            // };
    
            // XMLHttpRequest.prototype.setRequestHeader = function (header, value) {
            //     //console.log('setRequestHeader: XMLHttpRequest header:->',header + ": " + value);
            //     return this.xhr.setRequestHeader(header, value);
    
            // }
    
            // XMLHttpRequest.prototype.send = function (postBody) {
            //     let myXHR = this;
            //     //console.log('send: XMLHttpRequest header 注入token:->'+ token);
            //     this.xhr.setRequestHeader('token', token);
            //     this.xhr.onreadystatechange = function () { myXHR.onreadystatechangefunction() };
            //     this.xhr.send(postBody);
            // };
    
            // XMLHttpRequest.prototype.onreadystatechangefunction = function () {
            //     //console.log('onreadystatechangefunction:');
            //     try{
            //         this.readyState = this.xhr.readyState;
            //         this.responseText = this.xhr.responseText;
            //         this.responseXML = this.xhr.responseXML;
            //         this.status = this.xhr.status;
            //         this.statusText = this.xhr.statusText;
            //     }catch(e){
            //         console.log(e)
            //     }
                
            //     this.onreadystatechange();
    
            // };
    
            // iframeWin.XMLHttpRequest = XMLHttpRequest;
  • 相关阅读:
    settings.xml的配置
    查看Linux防火墙状态
    Linux系统安装jdk并配置环境变量
    Windows系统与虚拟机CentOS之间文件复制
    搭建公司后台服务架构(1)
    09-http.ts配置了全局的http拦截器,单独某个组件不想要这个拦截器,如何设置
    17- vue自定义指令-操作DOM的
    15-keep-alive
    14-观察者模式和发布订阅的区别/vue响应式是发布订阅模式和观察者模式
    13.每个vue文件都是一个私有作用域/css的私有性原理
  • 原文地址:https://www.cnblogs.com/yuri2016/p/14178665.html
Copyright © 2011-2022 走看看