Java: Use GET and POST in the same Form

1

What I want to do is send the parameters from Form to POST and then through the method GET consult the BBDD for room availability and send that result through Ajax (Vanilla Javascript) to HTML But from what I was seeing I can not use the methods at the same time. It only works if I only use the method POST (method="POST") or only the method GET (method="GET") on the Form.

Here is the query Ajax :

function loadDoc() {
                var xhttp = new XMLHttpRequest();
                xhttp.open("GET", "Controlador", true);
                xhttp.setRequestHeader('Content-Type', 'text/html');
                xhttp.onreadystatechange = function () {
                    if (this.readyState === 4 && this.status === 200) {
                        document.getElementById("precio").innerHTML = this.responseText;

                    }
                };

                xhttp.send();
                //document.getElementsByTagName("FORM").submit();
            }

And here the method doGet :

        PrintWriter out = response.getWriter();

        ReservaDAOImp rv = new ReservaDAOImp();

        rsv.setTipoHab(request.getParameter("tipHab"));
        rsv.setCodHotel(request.getParameter("hoteles"));
        out.print(rv.disponibilidad("select disponibilidad from T_tiphab where tipo=? and codHotel=?", rsv));
  

When I use the method GET in Form send me to another page with the   correct result but when I use it with Ajax the parameters of the   form arrive NULL to Servlet.

    
asked by TOMAS 13.03.2018 в 20:37
source

1 answer

1

Basically what you should do is capture the submit event of the form and do something in it. As an example we will show the following form

<form action="/action_page.php" method="post" onsubmit="funcionAdicional()">
...
</form>
<script>
    function funcionAdicional(){
        // ejecutar función  GET
        ...
    }
</script>

By adding the event handler onsubmit you can execute code when the form is sent.

    
answered by 14.03.2018 в 00:18