ReactJS、Golang 服务器 CORS 内容类型问题

ReactJS, Golang server CORS content-type trouble

所以我一直在尝试使用 POST 请求将我的 reactJS 本地客户端连接到我的 Golang 本地服务器,但我一直收到此错误:

Access to fetch at 'http://localhost:8080/' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.

我已经多次尝试更改服务器端和客户端的石南花,但到目前为止我没有成功。仅供参考,在 Postman 中一切正常。

这是我在客户端的代码

const requestOptions = {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                First: this.state.first,
                Second: this.state.second,
                Symbol: this.state.symbol,
                Date: this.state.date,
            })
        };
        fetch('http://localhost:8080/', requestOptions)
            .then(response => { return response.json() })
            .then(data => {
                this.setState({ result: data });
            })
        .catch(console.log)
}

这是在服务器端

type Params struct {
    First string
    Second string
    Symbol string
    Date string
}

func main() {

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

       if r.Method != http.MethodPost {
            fmt.Fprintf(w, "Hi there")
            return
        }


        w.Header().Set("Access-Control-Allow-Origin", "*")
        w.Header().Set("Content-Type", "application/json")

        var params Params
        err := json.NewDecoder(r.Body).Decode(&params)

        if err != nil {
            w.WriteHeader(http.StatusBadRequest)
            fmt.Fprintf(w, "%v", err)
            return
        }

        // somthing else here

    })

我做错了什么?

最后,我少了一个header。

w.Header().Set("Access-Control-Allow-Headers", "Content-Type")

归功于@mkopriva!