Answer the question
In order to leave comments, you need to log in
CORS policy: No 'Access-Control-Allow-Origin'?
Good afternoon/evening!
I understand that the question about CORS has already been raised many times, but I did not find the answer to my question.
There is a website written in REACTjs and there is a backend written in GOlang.
I don’t understand why one PUT method works, and the second PUT method does not work and gives an error
Access to XMLHttpRequest at 'http://localhost:8082/recipes/c4bab80a-14a8-426b-b808-8b4bf9574920' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
func setupCORS(w *http.ResponseWriter, r *http.Request) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
(*w).Header().Set("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, PATCH, DELETE")
(*w).Header().Set("Content-Type", "application/json")
(*w).Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
}
func (h *Handler) UpdateRecipe(w http.ResponseWriter, r *http.Request) {
setupCORS(&w, r)
if (*r).Method == "OPTIONS" {
return
}
// дальше идет код
static async putDishByID(changeDish) {
const response = await axios.put('http://localhost:8082/recipes/update/' + changeDish.id, {
//поля структры
})
Answer the question
In order to leave comments, you need to log in
Thanks to the help of vilinyh Alexander Pavlyuk Lynn "Kofeman"
The problem was inattention: the addresses of the requests did not match in the code either.
The issue was resolved.
Thank you all again.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question