1

I'm working with Java Web at Netbeans 8.1, actually I'm working with AJAX in the proyect and this issue stops me:

This is my JS function in JSP:

$("#formAddFundo").submit(function(event) {

 /* stop form from submitting normally */
 event.preventDefault();

 /* get the action attribute from the <form action=""> element */
 var $form = $(this),
     url = $form.attr('action');


 var status = 0;

 if (document.getElementById("chkFundo").checked)
     var status = 1;

 /* Send the data using post with element id name and name2*/
 var posting = $.post(url, {
     hIdFundo: $('#hIdFundo').val(),
     txtNombre: $('#txtNombre').val(),
     status: status,
     btnAddGrid: $('#btnAddGrid').val()
 });

 /* Alerts the results */
 posting.done(function(data) {

     $.get('addFundo', function(responseJson) {
         var t = $('#dt_basic').DataTable();
         t.clear();

         $.each(responseJson, function(index, objeto) { // Iterate over the JSON array.
             var estado;

             if (objeto.status === 1) {
                 estado = "Activo";
             } else {
                 estado = "No activo";
             }

             t.row.add([
                 objeto.idfundo,
                 objeto.nombre,
                 estado,
                 "<i class=\"btn btn-secondary fa fa-pencil\" onclick=\"update('" + objeto.nombre + "', '" + objeto.idfundo + "', '" + objeto.status + "');\"></i></a><a><i class=\"btn btn-secondary fa fa-trash-o\" onclick=\"borrar('" + objeto.nombre + "', '" + objeto.idfundo + "');\"></i></a>"
             ]).draw(false);
         });


         $('#alerta').html("<div class='alert alert-warning'><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Atención!</strong> " + <%= session.getAttribute("Respuesta") %> +" </strong></div>");
     });

     limpiar();
 });

 });

This is my servlet where JS goes to get response:

 String json = new Gson().toJson(listaFundos);

 response.setContentType("application/json");
 response.setCharacterEncoding("UTF-8");
 response.getWriter().write(json);

 request.getSession().setAttribute("Respuesta", resp);

Now. I need to print a HTML at (#alerta) in JSP without page refresh when POST method ends his function with Json. Session variables and scripplets only executes once at page refresh, not for further executions..

 $('#alerta').html("<div class='alert alert-warning'><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Atención!</strong> " + <%= session.getAttribute("Respuesta") %> +" </strong></div>");

0 Answers0