In my current setup, I have this code snippet to serve an HTML file via Go server.
func main() {
http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
path := r.URL.Path
if path == "/" {
path = "index.html"
}
http.ServeFile(rw, r, "./"+path)
})
http.ListenAndServe(":5555", nil)
}
The HTML file in question contains a JavaScript file that makes a fetch request to obtain some data. It functions correctly when served through apache but encounters an issue with the Go-server.
This is the fetch-request being made:
const fetchSettings = {
method: "POST",
body: JSON.stringify(requestBody),
headers: {
"Content-Type": "application/json",
}
};
const response = await fetch("https://some.url", fetchSettings);
The error message received is as follows:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://some.url. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://some.url. (Reason: CORS request did not succeed).