在Firefox中使用跨域脚本标记共享凭据

时间:2014-06-27 15:21:28

标签: javascript firefox cookies cross-domain

假设我已经在域boo.com中授权并参加此会话:

s:x2GYrvxGgmsfFwx7gDbKXmwB.4BDXLpMaSqD9hgeTkBDx3z4TeczJeC50gnrH5bc+kWU

我想要的是向域foo.com添加脚本标记,指向boo.com

<script src='http://boo.com/blah.js'></script>

在所有浏览器中,包括Safari和Google Chrome,脚本标记都会在HTTP标头中设置Cookie值,但它在Firefox中不起作用。

Firefox没有设置Cookie标头,因此我从服务器收到Unauthorized错误。问题是什么?

更新

我在服务器端启用了CORS,但问题仍然存在于Firefox中:

app.use(function (req, res, next) {
  // Website you wish to allow to connect
  res.setHeader('Access-Control-Allow-Origin', 'http://foo.com');

  // Request methods you wish to allow
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

  // Request headers you wish to allow
  res.setHeader('Access-Control-Allow-Headers', 'Cookie');

  // Set to true if you need the website to include cookies in the requests sent
  // to the API (e.g. in case you use sessions)
  res.setHeader('Access-Control-Allow-Credentials', true);

  // Pass to next layer of middleware
  next();
});

2 个答案:

答案 0 :(得分:1)

以下是解决方案:

<script src="http://boo.com/blah.js" crossorigin="anonymous"></script>

在这里阅读更多内容: http://blog.errorception.com/2012/12/catching-cross-domain-js-errors.html

答案 1 :(得分:1)

我发现了问题。由于我的Firefox不接受第三方Cookie,script标记和来自其他域的所有其他http请求都不会共享Cookie。

Here is the setting in Firefox

The option was <code>Never</code> before

相关问题