0

I want to loop through the list that the server response back to my ajax GET method using JSP c:forEach.

I tried looping through the response using for loop and creating elements for each field of the response like this

success : function(response) {
for (var i = 0; i < response.length; i++) {


    var current = response[i];
    var firstNameParagraph = document.createElement("p");
    firstNameParagraph.append(current.firstName);
    var lastNameParagraph = document.createElement("p");
    lastNameParagraph.append(current.lastName);
    var ageParagraph = document.createElement("p");
    ageParagraph.append(current.age);
    var sexParagraph = document.createElement("p");
    sexParagraph.append(current.sex);

    $("#div").append(firstNameParagraph, lastNameParagraph,
                                 ageParagraph, sexParagraph);
}

it's not good at all, it requires so many codes to create and put all the strings in table. but if I send it to the c:forEach, then I can do the same thing with a lot of fewer codes

this is the ajax get method

<script type="text/javascript">
    $(function() {

        $("#getMembers").click(function() {
            $.ajax({
                type : "GET",
                url : "http://localhost:8080/RCP2/members",
                data : "data",
                dataType : "json",
                success : function(response) {

                    //this response should go to c:forEach that i have in the same page.

                }

            });

        });

    });
</script>

and this is the forEach

<c:forEach var = i items="${i want the response to come here}">

    <p>${i.firstName}</p>
    <p>${i.lastName}</p>
    <p>${i.age}</p>
    <p>${i.sex}</p>


</c:forEach>    

if that's not possible, or if you know any better idea to achieve this goal, please feel free to tell me.

by the way, It's the first few hours that I'm working with javaScript and ajax, I'm just trying to make some a web pages for my rest server, so please be beginner-friendly. thanks in advance.

wazz
  • 4,953
  • 5
  • 20
  • 34
Amirreza Yegane
  • 315
  • 1
  • 5
  • 14
  • 1
    See "Returning List as XML" section of the duplicate. – BalusC Sep 23 '19 at 11:11
  • @BalusC we search for questions by the title, right? ok, compare the title of the link you came up with to the problem I'm having. – Amirreza Yegane Sep 23 '19 at 11:24
  • Uh? It's not a crime man :) – BalusC Sep 23 '19 at 11:35
  • @BalusC yeah you know, I got banned from asking a question for like 3 month after some of my questions marked as a duplicate. I was a bit scared to be banned from asking again. Anyway thanks for the answer and the link to the question, it really helped. – Amirreza Yegane Sep 23 '19 at 12:00
  • You won't be banned for asking too much duplicates. Only for too much low-quality or deleted questions. – BalusC Sep 23 '19 at 12:02
  • @BalusC that makes sense, because, I remember I deleted some of my questions. maybe that got me banned, thanks for clarification. – Amirreza Yegane Sep 23 '19 at 12:15

0 Answers0