美元AngularJS http。Internet Explorer

时间:2022-08-22 19:43:46

I have the following bit of code, which works fine in every modern browser, but fails in Internet Explorer 9 and below.

我有下面这段代码,它适用于所有现代浏览器,但在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 is an object that looks like this:

凭证是这样的对象:

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

The problem is that the POST never actually happens, instead it jumps straight to return err; and no error is even set. It says Impossible d'effectuer l'opération à cause de l'erreur suivante c00c023e.. The key being c00c023e, which is supposedly an encoding problem, but I don't see how that would completely prevent the call from happening.

问题是,这篇文章从来没有实际发生过,相反它直接跳转到返回错误;并且没有设置任何错误。它说不可能有更有效的操作来导致错误。关键是c00c023e,这可能是一个编码问题,但我不认为这会完全阻止调用的发生。

The 'fix' is to use $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; which does allow IE to POST, but my backend expects JSON so that doesn't help.

“修复”是使用$http.default .headers.post['Content-Type'] = 'application/x-www-form- urlencodes ';它允许IE发布,但我的后端期待JSON,所以这没有帮助。

I'm using AngularJS 1.2.22.

我用AngularJS 1.2.22。

Any ideas?

什么好主意吗?

1 个解决方案

#1


3  

Comments led me in the right direction, this was indeed a CORS issue. Turns out there is a simple and library agnostic fix provided by the XDomain library.

评论引导我向正确的方向前进,这确实是一个CORS问题。原来XDomain库提供了一个简单的和库无关的修复。

In the app making the CORS request, include the following script before any other scripts:

在提出CORS请求的app中,在其他脚本之前包含以下脚本:

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

Then, on the example.com domain, copy xdomain.min.js and create the proxy.html file that was defined above in the data-slave attribute. The proxy file must contain:

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

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

Cross-domain requests will now work properly in IE9 and below. You could use XDomain for every browser rather than just IE by removing the conditional comment, but I doubt there is a significant portion of users using a browser that doesn't support CORS that also isn't Internet Explorer.

跨域请求现在将在IE9和下面正常工作。你可以为每个浏览器使用XDomain,而不只是删除条件注释IE,但我怀疑有相当一部分用户使用的浏览器不支持CORS,也不支持Internet Explorer。

#1


3  

Comments led me in the right direction, this was indeed a CORS issue. Turns out there is a simple and library agnostic fix provided by the XDomain library.

评论引导我向正确的方向前进,这确实是一个CORS问题。原来XDomain库提供了一个简单的和库无关的修复。

In the app making the CORS request, include the following script before any other scripts:

在提出CORS请求的app中,在其他脚本之前包含以下脚本:

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

Then, on the example.com domain, copy xdomain.min.js and create the proxy.html file that was defined above in the data-slave attribute. The proxy file must contain:

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

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

Cross-domain requests will now work properly in IE9 and below. You could use XDomain for every browser rather than just IE by removing the conditional comment, but I doubt there is a significant portion of users using a browser that doesn't support CORS that also isn't Internet Explorer.

跨域请求现在将在IE9和下面正常工作。你可以为每个浏览器使用XDomain,而不只是删除条件注释IE,但我怀疑有相当一部分用户使用的浏览器不支持CORS,也不支持Internet Explorer。