第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為頁面上的所有AJAX請求添加“鉤子”

為頁面上的所有AJAX請求添加“鉤子”

牛魔王的故事 2019-07-26 14:42:41
為頁面上的所有AJAX請求添加“鉤子”我想知道是否可以“掛鉤”每個AJAX請求(無論是要發(fā)送還是事件)并執(zhí)行操作。此時我假設(shè)頁面上還有其他第三方腳本。其中一些可能使用jQuery,而另一些則不然。這可能嗎?
查看完整描述

3 回答

?
精慕HU

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個贊

受到aviv的回答的啟發(fā),我做了一些調(diào)查,這就是我想出來的。根據(jù)腳本中的注釋,
我不確定它是否有用,當(dāng)然只適用于使用本機(jī)XMLHttpRequest對象的瀏覽器。
我認(rèn)為如果javascript庫正在使用它將會起作用,因?yàn)槿绻赡艿脑捤鼈儗⑹褂帽緳C(jī)對象。

function addXMLRequestCallback(callback){
    var oldSend, i;
    if( XMLHttpRequest.callbacks ) {
        // we've already overridden send() so just add the callback
        XMLHttpRequest.callbacks.push( callback );
    } else {
        // create a callback queue
        XMLHttpRequest.callbacks = [callback];
        // store the native send()
        oldSend = XMLHttpRequest.prototype.send;
        // override the native send()
        XMLHttpRequest.prototype.send = function(){
            // process the callback queue
            // the xhr instance is passed into each callback but seems pretty useless
            // you can't tell what its destination is or call abort() without an error
            // so only really good for logging that a request has happened
            // I could be wrong, I hope so...
            // EDIT: I suppose you could override the onreadystatechange handler though
            for( i = 0; i < XMLHttpRequest.callbacks.length; i++ ) {
                XMLHttpRequest.callbacks[i]( this );
            }
            // call the native send()
            oldSend.apply(this, arguments);
        }
    }}// e.g.addXMLRequestCallback( function( xhr ) {
    console.log( xhr.responseText ); // (an empty string)});addXMLRequestCallback( function( xhr ) {
    console.dir( xhr ); // have a look if there is anything useful here});


查看完整回答
反對 回復(fù) 2019-07-26
?
猛跑小豬

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個贊

既然你提到的jQuery,我知道jQuery提供了一個.ajaxSetup()用于設(shè)置全局AJAX選項(xiàng),其中包括事件觸發(fā)類似方法success,errorbeforeSend-這是什么聽起來像你在找什么。

$.ajaxSetup({
    beforeSend: function() {
        //do stuff before request fires
    }});

當(dāng)然,您需要在嘗試使用此解決方案的任何頁面上驗(yàn)證jQuery可用性。


查看完整回答
反對 回復(fù) 2019-07-26
  • 3 回答
  • 0 關(guān)注
  • 730 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號