doucheng9058 2018-01-27 01:10
浏览 594
已采纳

Golang所请求的资源上没有'Access-Control-Allow-Origin'标头。 因此,不允许访问原始“空”

I am trying to test if I am in domain A, can domain A client send domain B cookie to domain B.

Here is my golang code

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)
    }
}

I first hit http://localhost:8012/set_cookie , then I open a html file containing a javascript using this library

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

The following happened

  1. Browser returns

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. My server print [] from fmt.Println(r.Cookies())

  2. If I hit http://localhost:8012/test_receive_cookie I can see the cookie I set gets print out on browser, but when I open a html that calls the endpoint the server will have empty cookie

My questions is how can I pass the cookie back to http://localhost:8012/test_receive_cookie using client code?

Am I missing some configuration code?

  • 写回答

1条回答 默认 最新

  • dongwei2983 2018-01-27 02:15
    关注

    You need your Go server to send the Access-Control-Allow-Origin headers, the client can't provide them (see CORS).

    If you update your server to add this header, the client will stop throwing an error.

    Example

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

    Reference:

    Enable CORS in Golang

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?