且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何拦截不同JS库发出的所有AJAX请求

更新时间:2021-08-20 07:09:04

这种类型的函数挂钩是非常安全的,并且由于其他原因在其他方法上定期执行.

This type of function hooking is perfectly safe and is done regularly on other methods for other reasons.

而且,唯一的性能影响实际上只是每个 .open() 的一个额外函数调用以及您自己执行的任何代码,这在涉及网络调用时可能无关紧要.

And, the only performance impact is really only one extra function call for each .open() plus whatever code you execute yourself which is probably immaterial when a networking call is involved.

在 IE 中,这不会捕获任何尝试使用 ActiveXObject 控制方法执行 Ajax 的代码.编写良好的代码首先查找 XMLHttpRequest 对象,并在可用时使用它,并且自 IE 7 以来一直可用.但是,可能有一些代码使用 ActiveXObject 方法,如果它可以通过 IE 的更高版本使用.

In IE, this won't catch any code that tries to use the ActiveXObject control method of doing Ajax. Well written code looks first for the XMLHttpRequest object and uses that if available and that has been available since IE 7. But, there could be some code that uses the ActiveXObject method if it's available which would be true through much later versions of IE.

在现代浏览器中,还有其他方法可以发出 Ajax 调用,例如 fetch() 接口 所以如果你想挂接所有的 Ajax 调用,你必须挂接的不仅仅是 XMLHttpRequest.

In modern browsers, there are other ways to issue Ajax calls such as the fetch() interface so if one is looking to hook all Ajax calls, you have to hook more than just XMLHttpRequest.