Error 405 - "Request not supported method"

-1

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 .

    
asked by anonymous 22.10.2018 / 15:54

1 answer

0

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

    
31.10.2018 / 14:35