我正在开发一个 Firefox 插件,并且正在监听这样的 http 响应:
var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.addObserver(httpObserver, "http-on-examine-response", false);
它调用我的 httpObserver 对象,如下所示:
var httpObserver =
{
observe : function(aSubject, aTopic, aData)
{
aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
httpstatus = aSubject.responseStatus;
if(httpstatus == 200 && aSubject.contentType.toString().indexOf("text/") != -1)
{
alert("URL: " + aSubject.name + "\r\nStatus: " + httpstatus + "\r\nContentType: " + aSubject.contentType); //Works great
alert("Body: " + aSubject.responseText); //Is undefined, why?
}
else if(httpstatus == 404 && aSubject.contentType.toString().indexOf("text/html") != -1)
alert("Page not found!\r\nURL: " + aSubject.name + "\r\nContentType: " + aSubject.contentType);
}
};
我遇到以下问题:
我想从响应中获取整个正文和 header ,但我不知道如何获取。 我曾经读到,可能是服务器不在同一个域中的问题,但是firefox如何处理它呢?
我已经做了很多研究,但没有发现任何有用的东西,也许我错过了一些东西..
顺便说一句:如果有帮助的话,我也可以访问请求对象。
最佳答案
您将使用nsITraceableChannel
interface为此,它由所有 HTTP 请求实现。这允许您用自己的 channel 监听器替换 channel 监听器,以便您可以在将 onDataAvailable
调用中读取数据,然后再将其传递到原始监听器。这里有一些示例代码就是这样做的: http://www.softwareishard.com/blog/firebug/nsitraceablechannel-intercept-http-traffic/
关于javascript - 获取响应 header /正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7712886/