Firefox 扩展 - 使用 XMLHttpRequest 的 cookie 访问不起作用


Firefox extension - cookie access using XMLHttpRequest not working

我有一个Firefox扩展,我正在使用Jetpack构建器创建。

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

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

问题是,php页面没有读取cookie。整个过程在Chrome中有效,但在FF中无效。问题不应该是跨域的,因为访问 cookie 的 PHP 页面与设置 cookie 的登录 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();

在发送之前,您可以尝试将此代码添加到您的请求中(从 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 */ }