获取dojo中所有AJAX调用的全局处理程序

获取dojo中所有AJAX调用的全局处理程序,ajax,dojo,Ajax,Dojo,在进行AJAX调用之前和AJAX调用成功之后(在调用实际的handler方法之前),我需要调用一些常用方法。我正在使用dojo.aspect来实现这一点 这是我的代码示例 function makeAjaxCall(){ dojo.xhrGet({ url:"sample_url", content:{ test:"value" }, load:function(response){

在进行AJAX调用之前和AJAX调用成功之后(在调用实际的handler方法之前),我需要调用一些常用方法。我正在使用
dojo.aspect
来实现这一点

这是我的代码示例

function makeAjaxCall(){
    dojo.xhrGet({
        url:"sample_url",
        content:{
            test:"value"
        },
        load:function(response){
            //Do some logic here
        },
        error:function(response){
            //handle error
        }
    });

}
下面是
dojo.aspect
,我使用它来获得
XHR
调用的挂钩

define(["dojo/aspect"], function(aspect){
     aspect.after(dojo, "xhr", function(deferred){
        console.log("AJAX AFTER");
        deferred.then(function(response){
            //CALLED AFTER 'load' METHOD IS CALLED.
            console.log("Testing");
        });
     });
    aspect.before(dojo, "xhr", function(method, args){

        console.log("AJAX BEFORE");
    });
});

现在问题是
延迟。然后在调用“
加载
”函数后调用
方面内的
。是否有可能在调用实际加载方法之前调用一个方法?

简短的回答是肯定的

首先,在Dojo中进行ajax调用有两种方法

  • dojo/xhr
    -这是您在上面拥有的,不推荐使用 赞成
  • dojo/request/xhr
  • 第一个实现将调用第二个实现。因此,我建议在
    dojo/request/xhr
    上使用aop

    aspect.around(require.modules['dojo/request/xhr'], 'result', function(originalXhr){
        return function(url, options, returnDeferred){
    
            var dfd = new Deferred();
    
            // Logic before making the xhr call
    
            originalXhr(url, options, returnDeferred)
                .then(function(response) {
    
                    // Logic handling the response but before resolving the deferred.
                    dfd.resolve(vm);
                    // Logic after resolving the deferred.
    
                }, function(err){
                    // error handling?
                    dfd.reject(msgs);
    
                }, function(update) {
                    dfd.progress(update);
            });
    
            return dfd;
        };
    }); 
    
    您可以在以下位置找到完整的实现: (~111行)

    用法:

    require('dojo/xhr/request', function(xhr){
        xhr({...}).then(
            function(response) {
                //handle response
            },
            function(error) {
                //handle error
            }
        );
    });
    

    dojo/xhr
    代码将自身转换为上述用法,因此您发布的代码应该可以工作。

    如果您切换到新的API-


    然后您可以使用和

    在Dojo1.10中有一个新的API来全局捕获请求的状态

    notify("error", function(error){
        console.error(error);
        //SyntaxError: Unexpected token < in JSON at position 0(…)
    });
    
    }))

    所以我发现json处理程序可以定制:

    require(["dojo/request/handlers"], function(handlers){
    
    handlers.register("json", function(response){
    
        if (response.status === 401) {
            window.location.reload();
            return;
        }
    
        return JSON.parse(response.text || null);
    });
    
    }))

    这样,您就可以在JSON.parse引发异常之前检测response.errors

    require(["dojo/request/handlers"], function(handlers){
    
    handlers.register("json", function(response){
    
        if (response.status === 401) {
            window.location.reload();
            return;
        }
    
        return JSON.parse(response.text || null);
    });