Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

218
Vistas
How to get the values from ajax to spring rest api controller

I am trying to get data from ajax to my spring rest API controller.

I have this image attached below. I am trying to delete or edit the data that the user selects.

enter image description here

JS file

    function submit(){
    if(document.getElementById('deleteButton').clicked == true)
    {
    var button="delete";
    }

    if(document.getElementById('editButton').clicked == true)
    {
    var button="edit";
    }

    var check = document.getElementsByName('check');
                var selected = new Array();
                for (var i=0; i<check.length; i++) {
                    if (check[i].checked) {
                        selected.push(check[i].value);
                    }
                }

                    $.ajax({
                        url:"/submit",
                        type:"POST",
                        contentType: "application/json",
                        dataType:'json',
                        data: {selected:selected,button:button},
                        success:function(data){
                          alert("SUCCESS");
                        },
                    });

    }

JSP File

    <h1 align="center">My Employees</h1>
<table border="1" cellpadding="10">
    <tr>
        <th></th>
        <th>Employee Number</th>
        <th>Name</th>
        <th>Address</th>
        <th>Phone Number</th>
    </tr>
    <c:forEach items="${emp}" var="emp">
        <tr>
            <td><input type="checkbox" name="check"/></td>  
            <td>${emp.id }</td>
            <td>${emp.name }</td>
            <td>${emp.address }</td>
            <td>${emp.phoneNo }</td>
        </tr>
    </c:forEach>
</table>

<button onclick="submit()" id="deleteButton" value="delete">Delete</button>
<button onclick="submit()" id="editButton" value="edit">Edit</button>

I am using ajax and js to know which button is clicked and then trying to send the values to the controller. But I don't know how to use it because I am a newbie in the front end.

All the help is appreciated, I am ready to provide all the details.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Giving a specific answer to your question is a pretty tall order. Since you mention Spring in the title, I'm assuming your back end is some sort of web app container that supports Spring and Java handlers.

The Java code you need to write would typically use some annotations to identify the controller class that will receive the Ajax requests, do whatever processing is necessary, and return a response body that the JavaScript can then interpret and display a result to the user.

Rather than try to guess what you would need, let me point you to Mkyong's Spring 4 MVC Ajax Hello World Example and Spring Boot Ajax example. Once you get into this, you will likely find you need examples of how to do specific things. You can find lots of examples at Mkyong's site. I also recommend the Baeldung.com tutorials on REST with Spring. There a several "hello world" examples to get you started plus specific tutorials on the many variations of how to make requests and receive responses.

Handling Ajax requests is very commonplace now and Spring has a lot of built-in handling of parsing incoming JSON and marshaling outgoing JSON back to the browser. You can still do it yourself and perhaps the first time you should, but I wouldn't fight what you more or less get for free.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda