摘要:卸载拦截卸载后,拦截将失效。最后测一下输出注意拦截函数返回值是一个,如果为则会阻断请求,默认为不会阻断请求。
你是否有过下面的需求:需要给所有ajax请求添加统一签名、需要统计某个接口被请求的次数、需要限制http请求的方法必须为get或post、需要分析别人网络协议等等,那么如何做?想想,如果能够拦截所有ajax请求,那么问题就会变的很简单!?,少年,想法有点大胆,不过,我欣赏!直接上轮子,Ajax-hook不仅可以满足你想要的,同时可以给你更多。
本博客原始地址:http://www.jianshu.com/p/9b634f1c9615
Ajax-hook源码地址 : https://github.com/wendux/Ajax-hook 欢迎star
一. 直接引入脚本
引入ajaxhook.js
拦截需要的ajax 回调或函数。
hookAjax({
//拦截回调
onreadystatechange:function(xhr){
console.log("onreadystatechange called: %O",xhr)
onload:function(xhr){
console.log("onload called: %O",xhr)
//拦截函数
open:function(arg){
console.log("open called: method:%s,url:%s,async:%s",arg[0],arg[1],arg[2])
ok, 我们使用jQuery(v3.1) 的get方法来测一下:
// get current page source code
$.get().done(function(d){
console.log(d.substr(0,30)+"...")
> open called: method:GET,url:http://localhost:63342/Ajax-hook/demo.html,async:true
> onload called: XMLHttpRequest
拦截成功了! 我们也可以看到jQuery3.1内部已经放弃onreadystatechange而改用onload了。
二. CommonJs下的模块构建工具环境中
假设在webpack下,第一步, 安装ajax-hook npm插件
npm install ajax-hook --save-dev
第二步,引入模块并调用api:
const ah=require("ajax-hook")
ah.hookAjax({
onreadystatechange:function(xhr){ ... },
onload:function(xhr){ ... },
ah.unHookAjax()
hookAjax(ob)
ob,类型是对象,key为想要拦截的回调或函数,value为我们的拦截函数。
返回值: 原始的 XMLHttpRequest。如果有写请求不想被拦截,可以new 这个。
unHookAjax()
卸载拦截;卸载后,拦截将失效。
改变ajax行为
拦截所有ajax请求,检测请求method,如果是“GET”,则中断请求并给出提示
hookAjax({
open:function(arg){
if(arg[0]=="GET"){
console.log("Request was aborted! method must be post! ")
return true;
拦截所有ajax请求,请求统一添加时间戳
hookAjax({
open:function(arg){
arg[1]+="?timestamp="+Date.now();
修改请求返回的数据“responseText”
hookAjax({
onload:function(xhr){
//请求到的数据首部添加"hook!"
xhr.responseText="hook!"+xhr.responseText;
hook!
有了这些示例,相信开篇提到的需求都很容易实现。最后测一下unHook
hookAjax({
onreadystatechange:function(xhr){
console.log("onreadystatechange called: %O",xhr)
//return true
onload:function(xhr){
console.log("onload called")
xhr.responseText="hook"+xhr.responseText;
//return true;
open:function(arg){
console.log("open called: method:%s,url:%s,async:%s",arg[0],arg[1],arg[2])
arg[1]+="?hook_tag=1";
send:function(arg){
console.log("send called: %O",arg[0])
$.get().done(function(d){
console.log(d.substr(0,30)+"...")
//use original XMLHttpRequest
console.log("unhook")
unHookAjax()
$.get().done(function(d){
console.log(d.substr(0,10))
open called: method:GET,url:http://localhost:63342/Ajax-hook/demo.html,async:true
send called: null
onload called
拦截函数返回值是一个boolean,如果为true则会阻断ajax请求,默认为false,不会阻断请求。
所有的回调拦截函数的参数为当前的XMLHttpRequest 实例,如onreadystatechange、onload;所有ajax原始方法的拦截函数会将原始参数以数组的形式传递给拦截函数,你可以在拦截函数中修改它。
相关链接:
Ajax-hook原理解析:http://www.jianshu.com/p/7337ac624b8e
BY THE WAY : 欢迎关注、star我的另一个开源项目 fly.js ! ?。
最后还是老话:刚开博客,求赞、求关注、求评论、各种求。
... // 全局通用组件目录
├── config // 项目配置,拦截器,开关
├── plugins // 插件相关,生成路由、请求、store 等实例,并挂载 Vue 实例
├── directives // 拓展指令集合
├── routes // 路由配置
├──...
rickchen
... // 全局通用组件目录
├── config // 项目配置,拦截器,开关
├── plugins // 插件相关,生成路由、请求、store 等实例,并挂载 Vue 实例
├── directives // 拓展指令集合
├── routes // 路由配置
├──...
Backache
...javascript运行环境;同时fly.js有一些高级的玩法如全局ajax拦截、在web app中支持请求重定向等,耐心看下去,它会给你足够的惊喜。
接下来会出几篇文章深入的介绍fly.js的高级玩法。这是首篇,一个整体的介绍,如果您有兴趣可...
OpenDigg
...样做!
// 把这段代码放在所有JS代码之前,我们就实现了拦截ajax的需求
window.XMLHttpRequest.prototype.open = (function(originOpen) {
return function(method, url, async) {
console.log("发送了ajax,url是: ", url);
return originOpen.apply(this...
Cheriselalala
阅读 254·2023-04-26 01:42
阅读 2965·2021-11-22 11:56
阅读 2107·2021-10-08 10:04
阅读 549·2021-09-24 10:37
阅读 8973·2021-09-22 15:49
阅读 2920·2019-08-30 15:52
阅读 1508·2019-08-29 13:44
阅读 279·2019-08-28 17:51