GoLang:将文件://的头部设置为null以进行http://请求不起作用

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

GoLang: Setting header to null for a file:// to http:// request not working

问题

这个问题涉及到在本地静态页面(file://)上使用Web服务器(http://)来提供文件,而不违反CORS。根据这个答案,可以使用一个Web服务器来为本地静态页面提供文件,而不违反CORS。根据这个答案,当从Web服务器发送数据到静态页面时,必须使用一个null的头部。但是下面的两行代码都不起作用,所以我该怎么做呢?

func handler(w http.ResponseWriter, r *http.Request) {
    w.Header().Add("Access-Control-Allow-Origin", nil) //这一行
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

func handler(w http.ResponseWriter, r *http.Request) {
    w.Header().Add("Access-Control-Allow-Origin", "")
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

第一段代码返回错误信息./main.go:42: cannot use nil as type string in argument to w.Header().Add

第二段代码可以编译通过,但是在客户端会抛出错误:Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/aardvark/posts. (Reason: CORS header 'Access-Control-Allow-Origin' missing)

英文:

This answer about static to static (file:// -> file://) states that a webserver (http://) can be used to serve files to a local static page (file://) without violating CORS. And this answer states that when sending data from a webserver to a static page, a header of null must be used. But neither of the two lines below are working, so how do I do it?

func handler(w http.ResponseWriter, r *http.Request) {
    w.Header().Add("Access-Control-Allow-Origin", nil) //this line
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

returns the error ./main.go:42: cannot use nil as type string in argument to w.Header().Add

func handler(w http.ResponseWriter, r *http.Request) {
    w.Header().Add("Access-Control-Allow-Origin", "")
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

This compiles but throws the client side error: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/aardvark/posts. (Reason: CORS header 'Access-Control-Allow-Origin' missing)

答案1

得分: 2

在撰写这个问题之后,我想尝试最后一招,出于绝望,结果奏效了。

func handler(w http.ResponseWriter, r *http.Request) {
    w.Header().Add("Access-Control-Allow-Origin", "null")
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

你应该将字符串设置为"null",而不是空字符串""nil

如果你认为这个问题不适合在SO上发布,请留下评论,我会尽快删除它。

英文:

After writing up this question, I thought to try one last thing out of desperation, and it worked.

func handler(w http.ResponseWriter, r *http.Request) {
    w.Header().Add("Access-Control-Allow-Origin", "null")
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

You're supposed to set the string to "null", rather than a null string "" or nil

If you don't think this question belongs on SO, please leave a comment and I'll promptly take it down.

huangapple
  • 本文由 发表于 2016年3月13日 12:39:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/35966549.html
匿名

发表评论

匿名网友

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

确定