【问题标题】:Firefox extension - cookie access using XMLHttpRequest not workingFirefox 扩展 - 使用 XMLHttpRequest 的 cookie 访问不起作用
【发布时间】:2012-06-08 01:58:57
【问题描述】:

我有一个使用 Jetpack builder 创建的 Firefox 扩展。

会发生什么,我在页面上的用户登录时设置了一个 cookie (PHP)。 cookie的内容是一个用来识别用户的特殊密钥,这个密钥可以获取用户的某些信息,以便插件工作。

我从 Firefox 插件向与登录页面位于同一服务器上的 php 页面发出 XMLHttpRequest。此页面必须读取 cookie,并据此检索数据。

问题是,cookie 没有被 php 页面读取。整个过程在 Chrome 中有效,但在 FF 中无效。问题应该不是跨域的,因为访问 cookie 的 PHP 页面与设置 cookie 的 Login PHP 页面在同一个域中。

请帮助...谢谢! :)

附:这是我的 XMLHttpRequest:

function getData() {
                client = new XMLHttpRequest();
                try{
                    //SHOULD I INCLUDE THE CODE IN 'TRY' IN HERE?
                    client.open('GET','https://www.istyla.com/Popup/themes.php');                   
                } catch (e){
                    alert( "error while opening " + e.message );
                }

                client.onreadystatechange = function(){
                    if (client.readyState ==4){
                            user_data = client.responseText;
                            if(document.domain == "facebook.com"     || document.domain == "www.facebook.com") {
                                addCSS(user_data);
                            }
                    }
                }

                client.send(null);
} getData();

【问题讨论】:

    标签: php html firefox cookies xmlhttprequest


    【解决方案1】:

    您可以尝试在发送之前将此代码添加到您的请求中(复制自https://addons.mozilla.org/sl/firefox/files/browse/134669/file/modules/sendtophone.js#L173)。

    // To send correctly cookies.
    // Force the request to include cookies even though this chrome code
    // is seen as a third-party, so the server knows the user for which we are
    // requesting favorites (or anything else user-specific in the future).
    // This only works in Firefox 3.6; in Firefox 3.5 the request will instead
    // fail to send cookies if the user has disabled third-party cookies.
    try {
      req.channel.QueryInterface(Ci.nsIHttpChannelInternal).
      forceAllowThirdPartyCookie = true;
    }
    catch(ex) { /* user is using Firefox 3.5 */ }
    

    【讨论】:

    • 我的问题中包含了我的 XMLHttpRequest。我把这段代码放在哪里?此代码是否允许 XMLHttpRequest 函数作为正在浏览的实际页面(即它可以访问 cookie)?
    猜你喜欢
    • 2017-05-26
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多