CORS error on same domain?(同一域上的CORS错误?)
问题描述
我现在遇到了一个奇怪的 CORS 问题.
I'm running into a weird CORS issue right now.
这是错误信息:
XMLHttpRequest cannot load http://localhost:8666/routeREST/select?q=[...]
Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin
两台服务器:
- localhost:8666/routeREST/:这是一个简单的 Python Bottle 服务器.
- localhost:8080/:Python simpleHTTPserver,我在其中运行 y Javascript 应用程序.此应用正在上面的服务器上执行 Ajax 请求.
有没有想过可能是什么问题?
Any thought on what could be the problem?
而且...端口是问题所在.谢谢你的回答:)
And... the port was the problem. Thanks for your answers :)
如果有人也在使用 Python 瓶服务器,您可以按照这篇文章中给出的答案来解决 CORS 问题:Bottle Py:为 jQuery AJAX 请求启用 CORS
If anyone is using a Python bottle server as well, you can follow the answer given on this post to solve the CORS issue: Bottle Py: Enabling CORS for jQuery AJAX requests
推荐答案
只有protocol、host 和才认为是相同的strong> port 相同:同源策略
It is only considered to be the same if the protocol, host and port is the same: Same Origin Policy
如果你想启用它,你必须遵循 跨域资源共享 (cors)通过添加标题.Mozilla 有 示例
If you want to enable it you must follow Cross-Origin Resource Sharing (cors) by adding headers. Mozilla has examples
您需要在响应中添加 Access-Control-Allow-Origin 作为标题.允许所有人(您可能不这样做):
You need to add Access-Control-Allow-Origin as a header in your response. To allow everyone (you should probably NOT do that):
Access-Control-Allow-Origin: *
如果您需要支持多个来源(例如 example.com
和 www.example.com
),请设置 Access-Control-Allow-Origin
在您对请求中 Origin
-header 的值的回复中(在您验证 Origin 已列入白名单之后.)
If you need to support multiple origins (for example both example.com
and www.example.com
), set the Access-Control-Allow-Origin
in your reply to the value of the Origin
-header from the request (after you verified that the Origin is white-listed.)
另外请注意,有些请求会发送一个带有 OPTION 方法的预检请求,因此如果您编写自己的代码,您也必须处理这些请求.请参阅 Mozilla 了解 示例.
Also note that some requests send a preflight-request, with an OPTION-method, so if you write your own code you must handle those requests too. See Mozilla for examples.
这篇关于同一域上的CORS错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:同一域上的CORS错误?
- Flexslider 箭头未正确显示 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Fetch API 如何获取响应体? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01