【问题标题】:XMLHttpRequest cannot load http://localhost:9090/receive. No 'Access-Control-Allow-Origin' header is present on the requested resourceXMLHttpRequest 无法加载 http://localhost:9090/receive。请求的资源上不存在“Access-Control-Allow-Origin”标头
【发布时间】:2015-01-02 21:02:19
【问题描述】:

我通过 nginx 服务器打开一个 html 文件,然后 html 文件将“POST”请求从 dropzone 传递到 nginx 服务器,然后 proxy_pass 到我的 go 服务器。然后这个 go 服务器接受请求。

但是当我尝试使用我的 html 文件并在 dropzone 中放置一些东西时,我得到了错误:

XMLHttpRequest cannot load http://localhost:9090/receive. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9009' is therefore not allowed access.

请帮帮我。

【问题讨论】:

  • 只需在您的服务器中添加Access-Control-Allow-Origin 标头。
  • 这个链接可能对enable-cors.org/server_nginx.html有帮助
  • 我无法理解为什么它会显示 CORS 问题,因为我使用的是同一台服务器“localhost”

标签: html nginx go


【解决方案1】:

在您上面的错误中,页面从http://localhost:9009 请求到http://localhost:9090/。根据此处的相同来源描述,这些是不同的来源:https://www.rfc-editor.org/rfc/rfc6454#section-5

来源必须匹配:

  • 方案
  • 主持人
  • 端口

对您而言,方案和主机相同,但端口不同。因此,您需要添加 CORS 标头以允许调用者在 http://localhost:9090 上调用您的服务器。

【讨论】:

    猜你喜欢
    • 2016-10-10
    • 2015-10-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 2017-08-30
    • 2014-12-26
    相关资源
    最近更新 更多