Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 交叉源请求已被阻止_Reactjs_Http_Go_Axios - Fatal编程技术网

Reactjs 交叉源请求已被阻止

Reactjs 交叉源请求已被阻止,reactjs,http,go,axios,Reactjs,Http,Go,Axios,我正在尝试从react with axios向我的golang microservice发送post请求,但我收到了一个错误 Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1:4040/register. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

我正在尝试从react with axios向我的golang microservice发送post请求,但我收到了一个错误

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1:4040/register. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
这是我的axios请求

const req = {
            data: [
                {
                    type : "register",
                    attributes : {
                        username : "Some name",
                        password : "asdasdasdasdasd3",
                        name : "some name",
                        email : "somename@yahoo.com"
                    }
                }
            ]
        }
axios.post(`http://127.0.0.1:4040/register`, { req })
            .then(res => {
                console.log(res);
                console.log(res.data);
        })
这是我在戈朗的终点

    func register (w http.ResponseWriter, r *http.Request) {
            w.Header().Set("Access-Control-Allow-Origin", "*")
            w.Header().Set("Access-Control-Allow-Credentials", "true")
            w.Header().Set("Content-Type", "application/json");
            jsonBody := registerController.Perform(r)
            w.Write(jsonBody)
    }

    func main() {
        router := mux.NewRouter().StrictSlash(true)

        fmt.Println("server running at port " + SERVER_PORT)
        router.HandleFunc("/register", register).Methods("POST")

        http.ListenAndServe(SERVER_PORT, router)
}

我想我遗漏了什么-有人能帮我解决这个问题吗

您的请求头似乎缺少CSRF参数

在MDN页面上阅读它,对我来说,似乎您需要在发送给微服务的请求中传递它


我相信您可以在这里找到解决方案: