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

huangapple go评论78阅读模式
英文:

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

问题

我正在通过nginx服务器打开一个HTML文件,然后该HTML文件通过dropzone将"POST"请求传递给nginx服务器,然后nginx服务器通过proxy_pass将请求转发给我的Go服务器。我的Go服务器接受该请求。

但是当我尝试使用我的HTML文件并在dropzone中放置一些内容时,我收到以下错误信息:

XMLHttpRequest无法加载http://localhost:9090/receive。所请求的资源上没有"Access-Control-Allow-Origin"头。因此,来自"http://localhost:9009"的源不被允许访问。

请帮助我解决这个问题。

英文:

I am opening a html file through nginx server and then the html file passes the "POST" request from the dropzone to the nginx server which then proxy_pass to my go server.This go server then accepts the request.

But when i try to use my html file and drop something in the dropzone i get the error :

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.

PLease help me out.

答案1

得分: 1

在你上面的错误中,你从http://localhost:9009加载页面,请求到http://localhost:9090/。根据同源策略的描述(参见https://www.rfc-editor.org/rfc/rfc6454#section-5),这两者是不同的源。

源必须匹配:

  • 协议
  • 主机
  • 端口

对于你来说,协议和主机是相同的,但是端口不同。因此,你需要添加CORS头,以允许调用者在http://localhost:9090上调用你的服务器。

英文:

In your error above you have the page loading from http://localhost:9009 requesting to http://localhost:9090/. These are different origins according to the Same Origin description here: https://www.rfc-editor.org/rfc/rfc6454#section-5

The origins must match:

  • scheme
  • host
  • port

For you the the scheme and host are the same, but the ports are different. Thus you will need to add the CORS headers to allow the caller to call your server on http://localhost:9090.

huangapple
  • 本文由 发表于 2015年1月1日 15:00:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/27728926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定