MENU

Golang 的一些问题

2019 年 10 月 06 日 • 玩机

跨域问题

Access to fetch at 'https://api.example1.com' from origin 'http://example2.com' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
# https://stackoverflow.com/questions/39507065/enable-cors-in-golang

func GetPeopleAPI(w http.ResponseWriter, r *http.Request) {

    //Allow CORS here By * or specific origin
    w.Header().Set("Access-Control-Allow-Origin", "*")

    w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
    // return "OKOK"
    json.NewEncoder(w).Encode("OKOK")
}

赞助博客

2016~2020 年经济学人合集 ( PDF+EPUB+MOBI )
赞助合集
经济学人 2020 年音频赞助版
赞助音频