【问题标题】:AngularJS $http.post error in Internet Explorer <= 9Internet Explorer <= 9 中的 AngularJS $http.post 错误
【发布时间】:2014-10-10 08:01:30
【问题描述】:

我有以下代码,在每个现代浏览器中都能正常工作,但在 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 发布,但我的后端需要 JSON,所以这没有帮助。

我正在使用 AngularJS 1.2.22。

有什么想法吗?

【问题讨论】:

  • 您是否要进行跨域请求?
  • 尝试发送一个 json ,类似于 {credentials:credentials}
  • 可能是这些浏览器不支持序列化为JSONstring,你自己试试吧:angular.toJson(credentials)
  • @ExplosionPills 是的,它是跨域的,但它适用于所有其他浏览器。
  • @KhanhTO 谢谢,我会试一试

标签: angularjs internet-explorer xdomainrequest


【解决方案1】:

评论把我引向了正确的方向,这确实是一个 CORS 问题。原来XDomain library 提供了一个简单且与库无关的修复程序。

在发出 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 并创建上面在data-slave 属性中定义的proxy.html 文件。代理文件必须包含:

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

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

【讨论】:

    猜你喜欢
    • 2013-06-06
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2011-04-25
    • 1970-01-01
    相关资源
    最近更新 更多