赞
踩
1.先执行下面的代码,实现全局拦截
- let origin = XMLHttpRequest.prototype.open;
-
- XMLHttpRequest.prototype.open = function (...args) {
- //在这里插入open拦截代码
- this.__on_response=function(res){return res}
- return origin.apply(this, args);
- };
- var accessor = Object.getOwnPropertyDescriptor(
- XMLHttpRequest.prototype,
- "response"
- );
-
- Object.defineProperty(XMLHttpRequest.prototype, "response", {
- get: function () {
- let response = accessor.get.call(this);
-
- //在__on_response里修改你的response
- response=this.__on_response(res)
-
- return response;
- },
- set: function (str) {
- return accessor.set.call(this, str);
- },
- configurable: true,
- });
- }
2.拦截示例代码
- let req=new XMLHttpRequest()
- req.__on_response=function(res){
-
- return res+"/modified"
- }
- req.open()
- req.send(method, url, async);
-
- console.log(req.response)
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。