原生xhr、fetch 请求的拦截。mock 技术原理

一、原生ajax对象【xhr、fetch】 请求的拦截: https://m.imooc.com/mip/wenda/detail/603075【通过改写send方法,使原有的send方法失效】   或  https://www.cnblogs.com/xiaoyuxy/p/12346344.html 【通过 自带的 中断 方法 实现。没有测试过】

  1、xhr 通过 XMLHttpRequest 的原型上 设置 send 方法,使得 原来的send 方法失效 ,就发送不了 请求了。

XMLHttpRequest.prototype.send = function () {
  //  在 XMLHttpRequest 原型上设置 了send 方法,原来的send 失效。
};

var xmlhttp = new XMLHttpRequest();
  xmlhttp.open("GET", "/try/ajax/demo_get.php", true);
  xmlhttp.send(); // xmlhttp.send() 自带 的发送行为 失效了。

  xmlhttp.onreadystatechange = function () {
    console.log("pp", xmlhttp.readyState);
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      console.log("dffa");
    }
  };

  猜想:mock.js 应该就是通过这种方式实现的。

二、mock.js

 
原文地址:https://www.cnblogs.com/wfblog/p/14142425.html