Error 405 - "Request not supported method"

Asked

Viewed 120 times

-1

The method you are sending by clicking the button is a GET but in my Javascript step as method POST and in the Controller he expects to receive a method POST also.

My JS:

function criaCategoria() {
  let tituloCategoria = document.getElementById("tituloCategoria").value
  data = {
    'tituloCategoria': tituloCategoria
  }

  fetch('/FlowerLiu/nota', {
    method: 'POST',
    body: JSON.stringify(data)
  })
}

My Controller to create category:

@RequestMapping(value = "/categoria", method = RequestMethod.POST)
public @ResponseBody String criaCategoria(@RequestBody String rawJson) {
  System.out.println("CRIO");
  JSONObject parsedJson = new JSONObject(rawJson);
  CategoriasDAO dao = new CategoriasDAO();
  Categoria categoria = new Categoria();
  categoria.setTitulo(parsedJson.getString("tituloCategoria"));
  dao.adicionaCategoria(categoria);
  dao.close();
  return "home";
}

My button on JSP:

<a href="categoria"><button onclick="criaCategoria()">Criar Categoria</button></a>

The moment I click on the "Create Category" button, I get the error:

405 - "Request not supported method"

as if the method sent was GET, but I expected a POST.

  • Welcome to Stackoverflow Victor. And at the moment the error occurs?

  • @Pedrogaspar The moment I click on the button to create, I get error 405 as if the sent method was GET but I expected a POST

1 answer

0

In your controller the url is "/category", but the request in fetch is for "/Flowerliu/note", change the url so that they are equal, the request must be accessing another existing resource.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.