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.