post call does not work, returns error 405

Asked

Viewed 86 times

0

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>

the cell in the 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 an error 405, as if trying to make 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.

1 answer

0

He may be making one get due to the passage of arguments ?nd1=. When posting, it’s best to send data to the payload, not the URL. Fix 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;
}
  • I’ll try to do as you said.?

  • Right here @Rafaelperracini, you say whether it worked or not.

  • not rolled, keeps giving the same problem...

  • Just to clarify, I need to use the pq button I make several calls for calculations from the same screen, so I don’t use Submit...I don’t know if the button needs to be inside a form, but I think not, since it calls the javascript function and spring bridges with the controller

Browser other questions tagged

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