title: Golang No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access
body: <p>I am trying to test if I am in domain A, can domain A client send domain B cookie to domain B.</p>
  
  <p>Here is my golang code</p>
  
  <pre><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, &amp;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)
      }
  }
  </code></pre>
  
  <p>I first hit <code>http://localhost:8012/set_cookie</code> , then I open a html file containing a javascript using this <a href="https://github.com/naugtur/xhr" rel="nofollow noreferrer">library</a></p>
  
  <pre><code>        this._xhr.get(
              "http://localhost:8012/test_receive_cookie", 
              { headers: { "Access-Control-Allow-Origin": "*" }},
              function(err, resp) {
                  console.log(resp);
                  console.log(err);
              });
  </code></pre>
  
  <p>The following happened</p>
  
  <ol>
  <li>Browser returns</li>
  </ol>
  
  <p><code>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.</code></p>
  
  <ol start="2">
  <li><p>My server print <code>[]</code> from <code>fmt.Println(r.Cookies())</code></p></li>
  <li><p>If I hit <code>http://localhost:8012/test_receive_cookie</code> 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</p></li>
  </ol>
  
  <p>My questions is how can I pass the cookie back to <code>http://localhost:8012/test_receive_cookie</code> using client code?</p>
  
  <p>Am I missing some configuration code?</p>