Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda