post call does not work, returns error 405

0

I have the following javascript

<script type="text/javascript">
            function calculaReducao(){
                var nd1 = document.getElementById("nrDentes1").value;
                var nd2 = document.getElementById("nrDentes2").value;
                $.post("calcularReducao?nd1=" + nd1 +"&nd2=" + nd2); 
            }        
</script>

cell in table

<td><input type="button" onclick="calculaReducao();" value="Executar" /></td>

and the controller

@RequestMapping(value = "/calcularReducao", method = RequestMethod.POST)
    public ModelAndView calcularReducao(double nd1, double nd2) throws IOException {

        ModelAndView mv = new ModelAndView("/calculos.jsp");
        double resultado = nd2/nd1;
        mv.addObject("reducao", resultado);
        return mv;

    }

And with all this, the click of the button returns me a 405 error, as if it were trying to get a get.

  

Message Request method 'GET' not supported
  Description The method received in the request-line is known by the origin server but not supported by the target resource.

    
asked by anonymous 20.11.2017 / 19:43

1 answer

0

It may be doing get because of passing arguments ?nd1= . When doing post, the ideal is to send data in the payload, not the URL. Correction for your case:

Javascript

<script type="text/javascript">
    function calculaReducao(){
        var nd1 = document.getElementById("nrDentes1").value;
        var nd2 = document.getElementById("nrDentes2").value;
        $.post("calcularReducao", { nd1: nd1, nd2: nd2 }); 
    }        
</script>

Spring controller

@RequestMapping(value = "/calcularReducao", method = RequestMethod.POST)
public ModelAndView calcularReducao(@RequestParam double nd1, @RequestParam double nd2) throws IOException {

    ModelAndView mv = new ModelAndView("/calculos.jsp");
    double resultado = nd2/nd1;
    mv.addObject("reducao", resultado);
    return mv;
}
    
20.11.2017 / 20:01