zoukankan      html  css  js  c++  java
  • 全局拦截各种http请求

    http请求无非就是ajax、src、href、表单

     function hookAJAX() {
        XMLHttpRequest.prototype.nativeOpen = XMLHttpRequest.prototype.open;
        var customizeOpen = function (method, url, async, user, password) {
          // do something
          this.nativeOpen(method, url, async, user, password);
        };
    
        XMLHttpRequest.prototype.open = customizeOpen;
      }
    
      /**
       *全局拦截Image的图片请求添加token
       *
       */
      function hookImg() {
        const property = Object.getOwnPropertyDescriptor(Image.prototype, 'src');
        const nativeSet = property.set;
    
        function customiseSrcSet(url) {
          // do something
          nativeSet.call(this, url);
        }
        Object.defineProperty(Image.prototype, 'src', {
          set: customiseSrcSet,
        });
      }
    
      /**
       * 拦截全局open的url添加token
       *
       */
      function hookOpen() {
        const nativeOpen = window.open;
        window.open = function (url) {
          // do something
          nativeOpen.call(this, url);
        };
      }
    
      function hookFetch() {
        var fet = Object.getOwnPropertyDescriptor(window, 'fetch')
        Object.defineProperty(window, 'fetch', {
          value: function (a, b, c) {
            // do something
            return fet.value.apply(this, args)
          }
        })
      }
  • 相关阅读:
    TP ajax
    TP分页
    TP表单验证
    TP数据删除
    TP数据查询
    TP【连接数据库配置及Model数据模型层】
    TP系统常量信息
    ThinkPHP中Session用法详解
    ThinkPHP部分内置函数
    element-ui select可搜索下拉框无法在IOS或Ipad调起小键盘输入法
  • 原文地址:https://www.cnblogs.com/amiezhang/p/9984690.html
Copyright © 2011-2022 走看看