The method you are sending when clicking the button is a GET
but in my JavaScript step as method POST
and in Controller
it expects to receive a POST
method as well.
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 in JSP:
<a href="categoria"><button onclick="criaCategoria()">Criar Categoria</button></a>
When I click the "Create Category" button, I get the error:
405 - "Request not supported method"
As if the method you sent was GET
, but I expected a POST
.