全局拦截各种http请求
Posted 飞翔的熊blabla
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了全局拦截各种http请求相关的知识,希望对你有一定的参考价值。
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)
)
以上是关于全局拦截各种http请求的主要内容,如果未能解决你的问题,请参考以下文章