我正在使用 mux 包并拥有以下代码:
func saveConfig(w http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("Origin"); origin != "" {
w.Header().Set("Access-Control-Allow-Origin", origin)
fmt.Println("Origin: " + 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")
}
// Stop here if its Preflighted OPTIONS request
if r.Method == "OPTIONS" {
return
}
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
fmt.Println("Error: %s\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
fmt.Println("JSON body:" + string(body))
if err := r.Body.Close(); err != nil {
panic(err)
}
w.WriteHeader(http.StatusCreated)
}
它在 IE 上运行良好,但 chrome preflight 正在发送一个 OPTIONS 方法,我收到了 404 响应。任何帮助将不胜感激。