一、ajax-hook插件
原理图
用法
引入文件
Using cdn
1
<script src="https://unpkg.com/ajax-hook/dist/ajaxhook.min.js"></script>
Using npm
1
npm install ajax-hook
注册回调函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17hookAjax({
//hook callbacks
onreadystatechange:function(xhr){
console.log("onreadystatechange called: %O",xhr)
},
onload:function(xhr){
console.log("onload called: %O",xhr)
},
//hook function
open:function(arg,xhr){
console.log("open called: method:%s,url:%s,async:%s",arg[0],arg[1],arg[2])
}
})
// NPM
// const ah=require("ajax-hook")
// ah.hookAjax({...})
异步请求
1
2
3
4// get current page source code
$.get().done(function(d){
console.log(d.substr(0,30)+"...")
})
结果
1
2
3
4
5> open called: method:GET,url:http://localhost:63342/Ajax-hook/demo.html,async:true
> onload called: XMLHttpRequest
>
<html>
<head l...
二、源码
1 | // index.js |
1 | // /src/ajaxhook.js |