Firefox setResponseHeader不起作用

时间:2014-11-15 13:17:23

标签: http-headers firefox-addon

我正在开发一个Web应用程序,我需要使用JavaScript访问iFrame的元素。为此,iFrame必须向浏览器发送“Allow-Control-Allow-Origin:*”标题。

不幸的是,这不会发生,这就是为什么我使用扩展来修改响应头,但由于某种原因,setResponseHeader不起作用。

由于我使用setResponseHeader剥离X-Frame-Options,因此更加令人困惑,但是当我设置自定义标题时,它将无法正常工作。

我正在使用Firefox的“Inspect Element”的“网络”选项卡来观察请求,虽然它显示正确设置了请求标头,但它没有显示响应标头。

这就是我设置请求和响应标头的方式。

var chrome = require("chrome");
chrome.Cc["@mozilla.org/observer-service;1"].getService( chrome.Ci.nsIObserverService ).addObserver({
    observe : function(subject, topic, data) {
            var channel = subject.QueryInterface( chrome.Ci.nsIHttpChannel );
            channel.setRequestHeader("x-mysite-extended", "somedata", false);
    }
},"http-on-modify-request",false);

chrome.Cc["@mozilla.org/observer-service;1"].getService( chrome.Ci.nsIObserverService ).addObserver({
    observe : function(subject, topic, data) {
            var channel = subject.QueryInterface( chrome.Ci.nsIHttpChannel );
            channel.setResponseHeader("x-mysite-extended", "somedata", false);
    }
},"http-on-examine-response",false);

同样,请求标头根据“网络”选项卡运行。我尝试了http-on-modify-request来设置响应头,但是这也没有用。

这就是我正在剥离X-Frame-Options标头的方法。

let myListener =
{
    observe : function (aSubject, aTopic, aData)
    {
      console.log(aTopic);
        if (aTopic == "http-on-examine-response")
        {
            let channel = aSubject.QueryInterface(Ci.nsIHttpChannel);

            try
            { // getResponseHeader will throw if the header isn't set

                let hasXFO = channel.getResponseHeader('X-Frame-Options');

                if (hasXFO)
                {
                    // Header found, disable it
                    channel.setResponseHeader('X-Frame-Options', '', false);
                }
            }
            catch (e) {}
        }
    }
}

var observerService = Cc["@mozilla.org/observer-service;1"]
                                .getService(Ci.nsIObserverService);
observerService.addObserver(myListener, "http-on-examine-response", false);

我一直试图解决这个问题两个小时,所以任何帮助都表示赞赏。感谢。

1 个答案:

答案 0 :(得分:1)

您正在为http-on-examine-response添加obserer,只有getResponseHeader

将其更改为http-on-modify-request。然后你可以setRequestHeader,但你不能getResponseHeader进入修改请求。

这是废码,但它对我有用:

        observe : function(aSubject, aTopic, aData) {
            // Make sure it is our connection first.
            if (aSubject == channel) {
                //this is our channel
                //alert('is my mine');
                cdxFire.myChannel = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
                if (cdxFire.myChannel.requestMethod == 'GET')   {
                    //alert('its a get so need to removeObserver now');
                    //cdxFire.observerService.removeObserver(modHeaderListener, "http-on-modify-request");
                }
                if (aTopic == 'http-on-modify-request' && cdxFire.myChannel.requestMethod == 'POST') {
                    //can set headers here including cookie
                    try {
                            var xContentLength = httpChannel.getRequestHeader('Content-Length');
                            var xContentType = httpChannel.getRequestHeader('Content-Type');
                            //alert('content length is there so change it up');
                            cdxFire.myChannel.setRequestHeader('Content-Type','',false);
                            cdxFire.myChannel.setRequestHeader('Content-Type',xContentType,false);
                            cdxFire.myChannel.setRequestHeader('Content-Length','',false);
                            cdxFire.myChannel.setRequestHeader('Content-Length',xContentLength,false);