我试图通过读取响应头来获取HTTP响应。但我无法找回任何东西。我到底错过了什么?这是我在 index.js
中的代码var {Cc, Ci} = require("chrome");
var httpRequestObserver =
{
init: function() {
var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
observerService.addObserver(this, "http-on-examine-response", false);
},
observe: function(subject, topic, data)
{
if (topic == "http-on-examine-response") {
subject.QueryInterface(Ci.nsIHttpChannel);
this.onExamineResponse(subject);
}
},
onExamineResponse: function (oHttp)
{
console.log("Header :"+JSON.stringify(oHttp));
try
{
var header_value = oHttp.getResponseHeader("<the_header_that_i_need>");
// URI is the nsIURI of the response you're looking at
// and spec gives you the full URL string
var url = oHttp.URI.spec;
}
catch(err)
{
console.log(err);
}
}
};
httpRequestObserver.init();
我从这个stackoverflow和几个在线博客中得到了参考:- Firefox add-on SDK: Get http response headers
被修改
我在控制台中检查了值。
答案 0 :(得分:0)
执行:
oHttp = oHttp.QueryInterface(Ci.nsIHttpChannel);
onExaminResponse
中的然后getRepsonseHeader
应该起作用
编辑:
这对我有用:
Services.obs.removeObserver(httpRequestObserver, "http-on-examine-response", false);
var httpRequestObserver =
{
init: function() {
Services.obs.addObserver(this, "http-on-examine-response", false);
},
observe: function(subject, topic, data)
{
subject = subject.QueryInterface(Ci.nsIHttpChannel);
this.onExamineResponse(subject);
},
onExamineResponse: function (oHttp)
{
//console.log("Header :"+JSON.stringify(oHttp));
var url = oHttp.URI.spec;
console.log('url:', url);
try
{
// URI is the nsIURI of the response you're looking at
// and spec gives you the full URL string
var header_value = oHttp.getResponseHeader("X-Firefox-Spdy");
console.log('header_value:', header_value);
}
catch(err)
{
if (err.name == 'NS_ERROR_NOT_AVAILABLE') {
console.log('this http request does not have header X-Firefox-Spdy')
} else {
console.error(err);
}
}
}
};
httpRequestObserver.init();