【问题标题】:Golang No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed accessGolang 请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'null' 不允许访问
【发布时间】:2018-07-06 09:35:06
【问题描述】:

我正在测试我是否在域 A 中,域 A 客户端是否可以将域 B cookie 发送到域 B。

这是我的 golang 代码

package main

import (
    "fmt"
    "net/http"
    "log"
    "time"
    "encoding/json"
)

func setCookie(w http.ResponseWriter, r *http.Request) {
    expiration := time.Now().Add(365 * 24 * time.Hour)
    cookie := http.Cookie{Path: "/test_receive_cookie", Name: "test_cors", Value: "test_cors", Expires: expiration}
    http.SetCookie(w, &cookie)
    fmt.Fprintf(w, "Success")
}

func receiveCookie(w http.ResponseWriter, r *http.Request) {
    fmt.Println(r.Cookies())

    data := make(map[string]interface{})
    for _, cookie := range r.Cookies() {
        data[cookie.Name] = cookie.Value
    }
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(data)
}

func main() {
    http.HandleFunc("/set_cookie", setCookie)
    http.HandleFunc("/test_receive_cookie", receiveCookie)
    err := http.ListenAndServe(":8012", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

我首先点击http://localhost:8012/set_cookie,然后使用library打开一个包含javascript的html文件

        this._xhr.get(
            "http://localhost:8012/test_receive_cookie", 
            { headers: { "Access-Control-Allow-Origin": "*" }},
            function(err, resp) {
                console.log(resp);
                console.log(err);
            });

发生了以下事情

  1. 浏览器返回

Failed to load http://localhost:8012/test_receive_cookie: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

  1. 我的服务器从fmt.Println(r.Cookies())打印[]

  2. 如果我点击http://localhost:8012/test_receive_cookie,我可以看到我设置的 cookie 会在浏览器上打印出来,但是当我打开一个调用端点的 html 时,服务器将有空 cookie

我的问题是如何使用客户端代码将 cookie 传回 http://localhost:8012/test_receive_cookie

我是否缺少一些配置代码?

【问题讨论】:

    标签: javascript go cookies


    【解决方案1】:

    您需要您的 Go 服务器发送 Access-Control-Allow-Origin 标头,客户端无法提供它们(请参阅 CORS)。

    如果您更新服务器以添加此标头,客户端将停止抛出错误。

    示例

    w.Header().Set("Content-Type", "text/html; charset=utf-8")
    w.Header().Set("Access-Control-Allow-Origin", "*")
    

    参考:

    Enable CORS in Golang

    【讨论】:

    • 所以 no origin 错误现在消失了,但是 r.Cookiesprintln 返回一个空数组。我不确定它为什么会这样做。因为我希望如果域名和路径名匹配,cookie 应该是自动附加的。
    • 这是我的请求&{GET /test_receive_cookie HTTP/1.1 1 1 map[Accept:[*/*] Accept-Encoding:[gzip, deflate, br] Accept-Language:[en-US,en;q=0.9] Here is my request. User-Agent:[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36] Connection:[keep-alive] Pragma:[no-cache] Cache-Control:[no-cache] Origin:[null]] {} <nil> 0 [] false localhost:8012 map[] map[] <nil> map[] [::1]:61817 /test_receive_cookie <nil> <nil> <nil> 0xc420014600}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 2014-03-13
    • 2016-05-27
    • 2013-12-24
    • 2018-10-23
    • 2017-07-17
    • 2017-02-04
    相关资源
    最近更新 更多