Variable step from js to java

0

I do not know how to catch a variable declared in javascript, from a java class.

Any suggestions?

var ratiosObj = {ratios:[{idRatio:1,Descripcion:"HoraMaxima",horas:10,Descuento:5}]};
var fila = 2;
var horas = "horas";
var horasFin;
var precioMax = "precioMax";
var precioMaxFin ;
$(document).ready(function(){
    ratiosObj.ratios.push({idRatio:1,Descripcion:"assdsds",});
    $("#add").on("click",function(){
        horasFin = "horas" + fila;
        precioMaxFin = "precioMax" + fila;
        $clone=$("#ratios tbody tr:first").clone();
        $clone.find("input:eq(0)").each(function(){
            $(this).val("");
            $(this).attr("name",horasFin);
        });
        $clone.find("input:eq(1)").each(function(){
            $(this).val("");
            $(this).attr("name",precioMaxFin);
        });
        $("#ratios tbody").append($clone);
        fila++;
    });
});

public static void modificarRatios (HttpServletRequest request, HttpServletResponse response, BaseWeb base) {
        HttpSession session = request.getSession();

        ZonasControlador zonaContr = new ZonasControlador(base.getVariablesGlobales());
        ParticipantePOJO vParticipante = (ParticipantePOJO)session.getAttribute("Participante");



        try{

        }catch(Exception ex){
            base.getLoggerGarageScanner().error("Se ha producido un error");
            request.setAttribute("errorpanelcontrol", "propietariocomunidaderrorcarga");
        }
        finally{

        }
        PropietarioWebControler.cargarcomunidades(request, response, base);
    }

I explain one how, the jquery, what you do is put rows to a table, but I need to put the name, in an object to pass it to the java class. I do not know how to do it, but this is the idea of how to do it

    
asked by urrutias 06.04.2017 в 13:05
source

1 answer

1

You have to understand two concepts:

  • Server side
  • Client side (browser)

Both are two separate environments where the only means of communication are HTTP requests. If you need to use a variable declared in the client on the server, you need to send it in an HTTP request to the server.

There are two ways to do this: by a normal request and by an asynchronous request. The first form is really simple, you only need to send the value of the variable by the URL or by the body of the request:

window.location = 'http://localhost:8080/controller/action?variable=valor

In case it is a POST request, it is done using a form:

<form 
  id="form1" 
  action="http://localhost:8080/controller/action" 
  method="POST"
>
  <input type="hidden" />
  <input type="submit" value="Enviar">
</form>

// código JS
document
  .getElementById('form1')
  .addEventListener('submit', function (e) {
    e.preventDefault()
    this.value = variableAEnviar
    this.submit()
  })

The other option is through an asynchronous request, that is, AJAX. This type of request has the peculiarity that you do not need to reload the document when sending the request and you get the server's response in text form (depending on the format in which you send it, it can be parsed).

fetch('http://localhost:8080/controller/action', {
  method: 'POST',
  body: JSON.stringify({ variable: variableAEnviar })
})
.then(res => res.json()) // en caso obtengas una respuesta en JSON
.then(data => console.log(data))
.catch(err => console.error(err))

There are multiple libraries for AJAX requests in addition to the standard fetch , such as request , superagent and axios .

    
answered by 07.04.2017 / 15:13
source