角力禁用CORS [英] Angular force disable CORS

查看:152
本文介绍了角力禁用CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过的角度(1.2.17)发送一个OPTION请求,如果我们在阿贾克斯设置一些自定义页眉上其他域(含$ HTTP)。 我知道这是一个RFC的某些部分地方(没必要指出这一点),但我会迫使反正发送实际的请求,因为用户将后面的3G网络在非洲的某个地方(你猜选项首部不会工作)。

I've seen that angular (1.2.17) is sending an OPTION request if we set some custom headers on another domain in ajax (with $http). I know this is some part of an RFC somewhere (no need to point that out) but I would force to send the actual request anyway because the users will be behind a 3G network somewhere in Africa (and you guess that the OPTION header will not work).

所以,问题肯定是在客户端,因为$阿贾克斯的作品,而不是$ HTTP。 我已经看到了计算器在这个问题上的其他线程,但我不希望配置服务器端,我要禁用它在角的客户端。 (请不要认为这一点)。

So the problem is definitely on the client side because $.ajax works and not $http. I've seen the other threads in stackoverflow on this issue, but I don't want to configure the server-side, I want to disable it on the client side in angular. (please don't suggest this).

在useXDomain选项我见过什么也不做和domainWhiteList的东西也不行。 有没有什么办法可以完全禁用这个东西? (或者我将不得不jQuery的使用,而不是为AJAX部分)。

the useXDomain option I've seen does nothing and the domainWhiteList thing does not work either. Is there any way to disable this thing entirely ? (or I will have to use jquery instead for the ajax part).

推荐答案

这不是AngularJS直接,它的OPTION请求时,它是由XHR完成。您可以检查的AngularJS源,AngularJS做没有明确执行的选项要求。

It is not AngularJS directly that does the OPTION request, it's done by XHR. You can check the AngularJS source, AngularJS does not explicitly perform an OPTION request.

根据 MDN ,XHR第一呢一个OPTION请求时:

According to MDN, XHR first does an OPTION request when:

      
  • 在它使用的方法不是GET,HEAD或POST等。此外,如果POST用于与内容类型比其它发送请求数据   应用程序/ x-WWW的形式urlen codeD,多部分/格式数据或text / plain的,   例如如果POST请求使用发送XML有效载荷到服务器   应用程序/ xml或text / xml的,则要求是pflighted $ P $。
  •   
  • 据请求设置自定义标题(如请求使用如X-PINGOTHER一个头)
  •   
  • It uses methods other than GET, HEAD or POST. Also, if POST is used to send request data with a Content-Type other than application/x-www-form-urlencoded, multipart/form-data, or text/plain, e.g. if the POST request sends an XML payload to the server using application/xml or text/xml, then the request is preflighted.
  • It sets custom headers in the request (e.g. the request uses a header such as X-PINGOTHER)

我注意到,AngularJS设置了POST请求的内容类型头应用程序/ JSON的;字符集= UTF-8 。我会尝试将其设置回文本/纯

I noticed that AngularJS sets the content type header for POST requests to application/json;charset=utf-8. I would try to set it back to text/plain.

 $httpProvider.defaults.headers.post = {'Content-Type': 'text/plain'}

如果这不起作用,我建议你检查的区别是,当一个请求由 $提出的请求头之间有什么。AJAX 和一个由 $ HTTP

If this doesn't work, I suggest you check what the differences are between the request headers when a request is made by $.ajax and one made by $http

这篇关于角力禁用CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆