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

时间:2012-06-03 20:41:38

标签: php html firefox cookies xmlhttprequest

我使用Jetpack构建器创建了一个Firefox扩展。

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

我将Firefox插件中的XMLHttpRequest发送到与登录页面相同的服务器上的php页面。此页面必须读取cookie,并根据该页面检索数据。

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

请帮忙......谢谢! :)

P.S。这是我的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();

1 个答案:

答案 0 :(得分:0)

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