S
S
SSSSTTTTAAAASSSS2022-02-14 19:48:26
go
SSSSTTTTAAAASSSS, 2022-02-14 19:48:26

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.

At the same time, GET, POST, DELETE requests work, one PUT also works, and the second one does not.
on the backend, in the handlers, CORS is registered everywhere through the call of the setupCORS function
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
  }
// дальше идет код

I make a request from the front through axios:
static async putDishByID(changeDish) {
        const response = await axios.put('http://localhost:8082/recipes/update/' + changeDish.id, {
//поля структры
        })

I don’t understand where to dig, and where to change something ...
If you have thoughts / ideas / tips, I will be very grateful)))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SSSSTTTTAAAASSSS, 2022-02-15
@SSSSTTTTAAAASSSS

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 question

Ask a Question

731 491 924 answers to any question