I am using the following code to serve a HTML file.
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)
}
This HTML file includes a JavaScript file that uses fetch to retrieve some data. This works fine when serving through apache, but not when served through the Go-server.
This is the fetch-request:
const fetchSettings = {
method: "POST",
body: JSON.stringify(requestBody),
headers: {
"Content-Type": "application/json",
}
};
const response = await fetch("https://some.url", fetchSettings);
And here is the error I'm getting:
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).