2

我有以下代码,在每个现代浏览器中都可以正常工作,但在 Internet Explorer 9 及更低版本中失败。

authService.login = function(credentials) {
  return $http.post('https://example.com/login', credentials).then(function(res) {
    return res;
  }, function(err) {
    return err;
  });
};

credentials是一个看起来像这样的对象:

{
  username: 'john@example.com',
  password: 'smith'
}

问题是 POST 从未真正发生过,而是直接跳转到return err;,甚至没有设置错误。它说Impossible d'effectuer l'opération à cause de l'erreur suivante c00c023e.。关键是c00c023e,这应该是一个编码问题,但我不明白这将如何完全阻止调用的发生。

“修复”是使用$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';允许 IE POST 的方法,但我的后端需要 JSON,所以这无济于事。

我正在使用 AngularJS 1.2.22。

有任何想法吗?

4

1 回答 1

3

评论让我朝着正确的方向前进,这确实是一个 CORS 问题。事实证明, XDomain 库提供了一个简单且与库无关的修复程序。

在发出 CORS 请求的应用程序中,在任何其他脚本之前包含以下脚本:

<!--[if lte IE 9]>
    <script src="http://example.com/xdomain/xdomain.min.js" data-slave="http://example.com/xdomain/proxy.html"></script>
<![endif]-->

然后,在example.com域上,复制xdomain.min.js并创建proxy.html上面在 data-slave属性中定义的文件。代理文件必须包含:

<!DOCTYPE html>
<script src="xdomain.min.js" data-master="https://subdomain.example.com"></script>

跨域请求现在可以在 IE9 及更低版本中正常工作。您可以通过删除条件注释将 XDomain 用于每个浏览器,而不仅仅是 IE,但我怀疑有很大一部分用户使用不支持 CORS 的浏览器,也不是 Internet Explorer。

于 2014-08-17T16:11:11.870 回答