title: How to solve "No 'Access-Control-Allow-Origin' header is present on the requested resource"
body: <p>I'm implementing REST API's in Go and for that I want to allow cross origin requests to be served.</p>
  
  <p>What I am currently doing:</p>
  
  <p>Go server code:</p>
  
  <pre><code>//handleCrossO ... This function will handle CROS
  func handleCrossO(w *http.ResponseWriter) {
  (*w).Header().Set("Content-Type", "application/json")
  (*w).Header().Set("Access-Control-Allow-Origin", "*")
  (*w).Header().Set("Access-Control-Allow-Methods", "POST, GET, 
       OPTIONS, PUT, DELETE")
  (*w).Header().Set("Access-Control-Allow-Headers", "Accept, 
       Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, 
       Authorization, Auth")
  }
  
  //Response ... This function will create response
  func Response(w http.ResponseWriter, message string, statusCode int) 
  {
  handleCrossO(&amp;w)
  w.WriteHeader(statusCode)
  w.Write([]byte("{\"message\":\"" + message + "\"}"))
  }
  </code></pre>
  
  <p>I am getting the following error on browser console:</p>
  
  <blockquote>
    <p>Access to XMLHttpRequest at '<a href="http://ip:8080/config" rel="nofollow noreferrer">http://ip:8080/config</a>' from origin '<a href="http://ip:4200" rel="nofollow noreferrer">http://ip:4200</a>' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.</p>
  </blockquote>
  
  <p>I have also tried the following code to check OPTIONS method:</p>
  
  <pre><code>// CheckAuthorization function check if the User is autrhorized to make calls or not
  // if ssid is mising then give unauthorized error otherwise call next
  func CheckAuthorization(next http.HandlerFunc) http.HandlerFunc {
  return func(w http.ResponseWriter, r *http.Request) {
      if r.Method == "OPTIONS" {
          //handle preflight in here
          response.Response(w, "success", 200)
      }else {
              store := session.SessionStore()
          session, _ := store.Get(r, utils.SessionName)
          ssid := r.Header.Get("Auth")
          if _, ok := session.Values[ssid]; ok {
              next.ServeHTTP(w, r)
          } else {
          var getTokenRes = GetTokenRes{}
          sendResponse(w, getTokenRes, 1, "Invalid 
                       SSID", 400)
          }
      }
  
  }
  }
  </code></pre>
  
  <p>But it is not working.</p>
  
  <p>I have also tried allow OPTIONS method:</p>
  
  <pre><code>router.HandleFunc("/network", authmiddleware.CheckAuthorization(createConfiguration)).Methods("POST", "OPTIONS")
  </code></pre>