Func Main () {Openhttplisten ()}func Openhttplisten () {http. Handlefunc ("/", Receiveclientrequest) fmt. PRINTLN ("Go server start running ...") Err: = http. Listenandserve (": 9090", nil) if err! = Nil {log. Fatal ("Listenandserve:", err)}}func receiveclientrequest (w http. Responsewriter, R *http. Request) {W.header (). Set ("Access-control-allow-origin", "*") //Allow access to all domain W. Header (). ADD ("Access-control-allow-headers", "Content-type")//header the type W. Header (). Set ("Content-type", "Application/json") //Return data format is JSONR. Parseform () fmt. Println ("Received client request:", R.form)
Test down, found that the web to send data to the Go server can be received, the go server returned data to the client Web side, there is a cross-domain error prompts, to HTTP. Responsewriter.header () Add these three lines of code:
W.header (). Set ("Access-control-allow-origin", "*") //Allow access to all domain W. Header (). ADD ("Access-control-allow-headers", "Content-type")//header the type W. Header (). Set ("Content-type", "Application/json") //Return Data format is JSON
Golang http Server cross-domain problem resolution