0

我在 http-on-examine-response 上定义了一个观察者

httpRequestObserver = {

observe: function(request, aTopic, aData){

        if (aTopic == "http-on-examine-response") {
             request.QueryInterface(Ci.nsIHttpChannel);

             var newListener = new TracingListener();
             request.QueryInterface(Ci.nsITraceableChannel);

             newListener.originalListener = request.setNewListener(newListener);

    }
}, 
QueryInterface: function(aIID){
    if (typeof Cc == "undefined") {
        var Cc = components.classes;
    }
    if (typeof Ci == "undefined") {
        var Ci = components.interfaces;
    }
    if (aIID.equals(Ci.nsIObserver) ||
    aIID.equals(Ci.nsISupports)) {
        return this;
    }

    throw components.results.NS_NOINTERFACE;

},
};

我只想在响应的响应代码为 200 的情况下添加侦听器。基本上我想要完成的是避免 3xx 响应。但我无法从标题中读取 ResponseCode。如何在此处获取响应代码.

在此先感谢您的帮助...

4

1 回答 1

1

你可以使用httpChannel.responseStatus,这里是文档

因此,将您的observe功能更改为

observe: function(request, aTopic, aData){
        if (aTopic !== 'http-on-examine-response') {
            return;
        }
        let httpChannel = request.QueryInterface(Ci.nsIHttpChannel);
        if (httpChannel.responseStatus !== 200) {
            return;
        }

        // do your stuff...
        let traceableChannel = event.subject.QueryInterface(Ci.nsITraceableChannel);
        let newListener = new TracingListener();
        newListener.originalListener = traceableChannel.setNewListener(newListener);

    }
}, 
于 2013-04-21T01:04:43.470 回答