【问题标题】:Is there a way to avoid Preflighting with $http?有没有办法避免使用 $http 进行预检?
【发布时间】:2013-08-12 05:40:33
【问题描述】:

我在远程服务器上使用 nginx,并且不支持 OPTIONS 方法,我被卡住了。服务器和 Angular 都拒绝互相交谈。

我只想发出一个简单的 $http.post() 请求。有没有办法将服务配置为仅发送 POST 请求,而不使用 OPTIONS 进行任何预检?

【问题讨论】:

    标签: angularjs cross-domain angularjs-service


    【解决方案1】:

    如果 CORS 是不可避免的 您可以将请求的标头更改为 text/plain,然后根据下面此链接中的答案手动解析您的响应

    How to skip the OPTIONS preflight request in AngularJS

    【讨论】:

      【解决方案2】:

      这不是 AngularJS 所做的事情,而是您的浏览器根据跨域资源共享标准所做的事情。 See also this answer on a related issue.

      但是,如果您使 AngularJS 应用程序从与您的资源相同的域提供服务(不同的子域将影响跨域),那么浏览器将不会发送 OPTIONS 请求,因为资源不再来自跨域服务器。

      例子:

      • www.example.com 请求 api.example.com 上的资源将触发 OPTIONS 请求
      • www.example.com 从 www.example.com/api 请求资源将不会触发 OPTIONS 请求

      【讨论】:

      • 在我目前的情况下,跨域是不可避免的:(我还有其他选择吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 2019-09-07
      • 1970-01-01
      • 1970-01-01
      • 2019-05-29
      • 2011-09-13
      相关资源
      最近更新 更多