JS 攔截全局ajax請求實例解析
你是否有過下面的需求:需要給所有ajax請求添加統(tǒng)一簽名、需要統(tǒng)計某個接口被請求的次數(shù)、需要限制http請求的方法必須為get或post、需要分析別人網(wǎng)絡(luò)協(xié)議等等,那么如何做?想想,如果能夠攔截所有ajax請求,那么問題就會變的很簡單!😄,少年,想法有點大膽,不過,我欣賞!直接上輪子,Ajax-hook不僅可以滿足你想要的,同時可以給你更多。
Ajax-hook源碼地址 : https://github.com/wendux/Ajax-hook
如何使用
1.引入ajaxhook.js
<script src="wendu.ajaxhook.js"></script>
2.攔截需要的ajax 回調(diào)或函數(shù)。
hookAjax({ //攔截回調(diào) onreadystatechange:function(xhr){ console.log("onreadystatechange called: %O",xhr) }, onload:function(xhr){ console.log("onload called: %O",xhr) }, //攔截函數(shù) 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)+"...") })
結(jié)果 :
> open called: method:GET,url:http://localhost:63342/Ajax-hook/demo.html,async:true > onload called: XMLHttpRequest > <!DOCTYPE html> <html> <head l...
攔截成功了! 我們也可以看到j(luò)Query3.1內(nèi)部已經(jīng)放棄onreadystatechange而改用onload了。
API
hookAjax(ob)
- ob,類型是對象,key為想要攔截的回調(diào)或函數(shù),value為我們的攔截函數(shù)。
- 返回值: 原始的 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請求,請求統(tǒng)一添加時間戳
hookAjax({ open:function(arg){ arg[1]+="?timestamp="+Date.now(); } })
修改請求返回的數(shù)據(jù)“responseText”
hookAjax({ onload:function(xhr){ //請求到的數(shù)據(jù)首部添加"hook!" xhr.responseText="hook!"+xhr.responseText; } })
結(jié)果:
hook!<!DOCTYPE html> <html> <h...
有了這些示例,相信開篇提到的需求都很容易實現(xiàn)。最后測一下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 hook<!DOCTYPE html> <html> <he... unhook <!DOCTYPE
注意
1.攔截函數(shù)返回值是一個boolean,如果為true則會阻斷ajax請求,默認為false,不會阻斷請求。
2.所有的回調(diào)攔截函數(shù)的參數(shù)為當前的XMLHttpRequest 實例,如onreadystatechange、onload;所有ajax原始方法的攔截函數(shù)會將原始參數(shù)以數(shù)組的形式傳遞給攔截函數(shù),你可以在攔截函數(shù)中修改它。
以上所述是小編給大家介紹的JS 攔截全局ajax請求實例解析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
使用JS輕松實現(xiàn)ionic調(diào)用鍵盤搜索功能(超實用)
這篇文章主要介紹了使用JS輕松實現(xiàn)ionic調(diào)用鍵盤搜索功能(超實用)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09Flutter 超實用簡單菜單彈出框 PopupMenuButton功能
這篇文章主要介紹了Flutter 超實用簡單菜單彈出框 PopupMenuButton功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08jquery+css3實現(xiàn)網(wǎng)頁背景花瓣隨機飄落特效
在qq空間可以自定義一些插件,裝飾空間,大家通常就是復(fù)制代碼到空間粘貼,會實現(xiàn)非常美觀的效果,有的會隨機飄落一些花瓣,那么這種效果用代碼怎么實現(xiàn)的呢,下面小編給大家詳解jquery實現(xiàn)網(wǎng)頁背景花瓣隨機飄落特效,需要的朋友可以參考下2015-08-08JS中map與forEach無法跳出循環(huán)及every和some的使用
在我們平時使用習(xí)慣中,for循環(huán)里要跳出整個循環(huán)是使用break,但在數(shù)組中用forEach循環(huán)或者map如要退出整個循環(huán)使用break會報錯,使用return也不能跳出循環(huán),下面這篇文章主要介紹了關(guān)于JS中map與forEach無法跳出循環(huán)及every和some的使用的相關(guān)資料,需要的朋友可以參考下2023-05-05使用微信小程序API,調(diào)用微信的各種內(nèi)置能力。
微信小程序如何使用小程序API,去調(diào)用微信提供的各種內(nèi)置能力(即微信API)。小程序開發(fā)框架提供豐富的微信原生API,可以方便的調(diào)起微信提供的能力,如獲取用戶信息,本地存儲,支付功能等。2022-12-12