同一域上的CORS错误? [英] CORS error on same domain?

查看:16
本文介绍了同一域上的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

推荐答案

只有protocolhost 和才认为是相同的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.comwww.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错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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