本文翻译自:Response to preflight request doesn't pass access control check

I'm getting this error using ngResource to call a REST API on Amazon Web Services: 我在使用ngResource调用Amazon Web Services上的REST API时遇到此错误:

XMLHttpRequest cannot load http://server.apiurl.com:8000/s/login?login=facebook . XMLHttpRequest无法加载http://server.apiurl.com:8000/s/login?login=facebook 。 Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 对预检请求的响应未通过访问控制检查:请求的资源上不存在“ Access-Control-Allow-Origin”标头。 Origin ' http://localhost ' is therefore not allowed access. 因此,不允许访问源“ http:// localhost ”。 Error 405 错误405

Service: 服务:

socialMarkt.factory('loginService', ['$resource', function($resource){    var apiAddress = "http://server.apiurl.com:8000/s/login/";return $resource(apiAddress, { login:"facebook", access_token: "@access_token" ,facebook_id: "@facebook_id" }, {getUser: {method:'POST'}});
}]);

Controller: 控制器:

[...]
loginService.getUser(JSON.stringify(fbObj)),function(data){console.log(data);},function(result) {console.error('Error', result.status);}
[...]

I'm using Chrome, and I dont know what else to do in order to fix this problem. 我正在使用Chrome,但我不知道该怎么做才能解决此问题。 I've even configured the server to accept headers from origin localhost . 我什至将服务器配置为接受源localhost标头。


#1楼

参考:https://stackoom.com/question/2PKFH/对预检请求的响应未通过访问控制检查


#2楼

You are running into CORS issues. 您遇到了CORS问题。

There are several ways to fix/workaround this. 有几种方法可以解决此问题。

  1. Turn off CORS. 关闭CORS。 For example: how to turn off cors in chrome 例如: 如何关闭Chrome中的cors
  2. Use a plugin for your browser 使用浏览器插件
  3. Use a proxy such as nginx. 使用代理,例如nginx。 example of how to set up 如何设置的例子

More verbosely, you are trying to access api.serverurl.com from localhost. 更详细地说,您正在尝试从本地主机访问api.serverurl.com。 This is the exact definition of cross domain request. 这是跨域请求的确切定义。

By either turning it off just to get your work done (OK, put poor security for you if you visit other sites and just kicks the can down the road) you can use a proxy which makes your browser think all requests come from local host when really you have local server that then calls the remote server. 通过关闭它来完成您的工作(好吧,如果您访问其他站点并给您带来安全隐患,那么您可以使用代理)使您的浏览器认为所有请求都来自本地主机。确实,您有本地服务器,然后再调用远程服务器。

so api.serverurl.com might become localhost:8000/api and your local nginx or other proxy will send to the correct destination. 因此api.serverurl.com可能会变为localhost:8000 / api,并且您的本地nginx或其他代理将发送到正确的目的地。


Now by popular demand, 100% more CORS info ....same great taste! 现在,由于受欢迎的需求, CORS信息增加了100% 。


And for the downvoters.... bypassing CORS is exactly what is shown for those simply learning the front end. 对于不愿花钱的人……绕过CORS正是那些仅仅学习前端的人所看到的。 https://codecraft.tv/courses/angular/http/http-with-promises/ https://codecraft.tv/courses/angular/http/http-with-promises/


#3楼

My "API Server" is an PHP Application so to solve this problem I found the below solution to work: 我的“ API服务器”是一个PHP应用程序,因此为了解决此问题,我找到了以下解决方案:

Place the lines in index.php 将行放在index.php中

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');

#4楼

For python flask server, you can use the flask-cors plugin to enable cross domain requests. 对于python flask服务器,您可以使用flask-cors插件启用跨域请求。

See : https://flask-cors.readthedocs.io/en/latest/ 参见: https : //flask-cors.readthedocs.io/en/latest/


#5楼

I have faced with this problem when DNS server was set to 8.8.8.8 (google's). 当DNS服务器设置为8.8.8.8(谷歌的)时,我遇到了这个问题。 Actually, the problem was in router, my application tried to connect with server through the google, not locally (for my particular case). 实际上,问题出在路由器上,我的应用程序尝试通过Google而不是本地与服务器连接(对于我的特定情况)。 I have removed 8.8.8.8 and this solved the issue. 我删除了8.8.8.8,这解决了问题。 I know that this issues solved by CORS settings, but maybe someone will have the same trouble as me 我知道可以通过CORS设置解决此问题,但也许有人会遇到与我相同的麻烦


#6楼

In my Apache VirtualHost config file, I have added following lines : 在我的Apache VirtualHost配置文件中,添加了以下几行:

Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]

对预检请求的响应未通过访问控制检查相关推荐

  1. 对飞行前请求的响应未通过访问控制检查:它没有http ok状态。_对不起,看完这篇HTTP,真的可以吊打面试官...

    点击上方"码农沉思录",选择"设为星标" 优质文章,及时送达 HTTP 内容协商 什么是内容协商 在 HTTP 中,内容协商是一种用于在同一 URL 上提供资源 ...

  2. 对CORS OPTIONS预检请求的一些思考

    前后端分离模大势所趋,跨域问题更是老生常谈. <程序员应对浏览器同源策略的姿势>一文提到三种跨域请求方案,重点讲述了w3c和浏览器厂商推出的CORS规范. 同源策略  所谓同源是指域名.协 ...

  3. 浏览器预检请求返回400 has been blocked by CORS policy: Response to preflight request doesn’t pass access cont

    这个问题也是很过分头一次遇到,原因是谷歌浏览器在有跨域(CORS)请求时,会先发送一个preflight(预检)请求,之后才会发送fetch请求. CORS:跨源资源共享 (CORS)(或通俗地译为跨 ...

  4. 从前后端的角度分析options预检请求——打破前后端联调的理解障碍

    文章目录 1.从前端的角度看options--post请求之前一定会有options请求?信口雌黄! 2.从后端的角度看options--post请求之前一定会有options请求?胡说八道! 1.从 ...

  5. CORS预检请求详谈

    引言 最近在项目中因前后端部署不同地方,前端在请求后端api时发生了跨域请求,我们采用CORS(跨域资源共享)来解决跨域请求,这需要前后端的配合来完成.在这一过程中,后端支持了CORS跨域请求后,前端 ...

  6. CORS - 引入预检请求的动机是什么?

    本文翻译自:CORS - What is the motivation behind introducing preflight requests? Cross-origin resource sha ...

  7. OPTIONS预检请求

    1.options请求是什么?什么时候浏览器会发送预检请求 options是预检请求,在真正的请求发送出去之前,浏览器都会先发送一个options请求 向服务器询问此接口是否允许我访问.浏览器在当前真 ...

  8. Chrome98和Chrome101的跨域变化,httpOPTIONS预检请求,私有网络访问限制

    在Chrome94更新时,发现访问本地服务器的时候谷歌浏览器限制了访问本地资源 当时通过一个浏览器设置进行了处理.但是治标不治本,98版本更新后又出现了CORS跨域问题.查询了一下资料: Chrome ...

  9. 什么时候会发送options请求(预检请求)

    OPTIONS请求即预检请求,可用于检测服务器允许的http方法.当发起跨域请求时,由于安全原因,触发一定条件时浏览器会在正式请求之前自动先发起OPTIONS请求,即CORS预检请求,服务器若接受该跨 ...

最新文章

  1. 一文拆解中国火星车着陆全过程
  2. weblogic缓存导致的网页验证码无法获取到
  3. cd库图像处理php,PHP图像处理类库及演示分享
  4. 2.3.5 操作系统之信号量机制实现进程的互斥、同步与前驱关系
  5. c语言铁道,C语言程序设计(方少卿) 铁道C第8章(修订版).pdf
  6. C语言基本运算符和表达式
  7. Price merge是通过org change的callback来trigger的
  8. Android开发之高斯模糊效果三行代码搞定附带CSDN源码请导入module
  9. Redis底层实现--字符串
  10. AirtestIDE 教程 — 5分钟上手自动化测试
  11. JavaScript中Object.keys、Object.getOwnPropertyNames区别
  12. 如何配置Ubuntu 16.04 GRUB 2引导加载程序
  13. .NET core ABP 获取远程IP地址
  14. 结构体变量偏移量及大小计算
  15. python爬楼梯递归_爬楼梯(Python3)
  16. C/C++后端开发学习路线总结(附带实习学习经历分享)
  17. ictclas4j java_使用ICTCLAS JAVA版(ictclas4j)进行中文分词
  18. 华为android内存扩大,安卓手机运行内存越来越不够用,华为却放出了这一招来解决!...
  19. Google word/sheets 常见的使用:
  20. vue的头像生成神奇

热门文章

  1. POJ 3276 Face The Right Way 反转
  2. linux vi 中s 替换方法
  3. socket、listen 等函数的打电话隐喻
  4. OPEN×××拨入后给不同的用户分配不同的访问权限
  5. APP被苹果App Store拒绝的原因
  6. 转帖:李开复的“创新工厂”为何失败?原作者QuarterThousand
  7. 强大的DataGrid组件[12]_分组(Group)——Silverlight学习笔记[20]
  8. 最近在整理和准备发布
  9. HelloDjango 第 10 篇:小细节 Markdown 文章自动生成目录,提升阅读体验
  10. ROS软路由的基本操作