Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

please help me.

i am new in JS and need help for a response Array:

In the following URL are 5 eMails. what is the correct way to get all emails from the 5 objects from:

var root = "https://jsonplaceholder.typicode.com/comments";

In my Code i get only the first email:

What I have tried:

var root = "https://jsonplaceholder.typicode.com/comments";

    $.ajax (root, {
        dataType: "json",
        method: 'GET',
        success: function(response){
            var userId = response[0].postId;
            var title = response[0].email;
           // var title = $(response).filter('email');

            var $info = $("<p></p>");
            $info.text("Post id is: " + userId + " email is " + title);
            $("div.main").append($info);
            console.log(response);
        },
        error: function(request,errorType, errorMsg){
            alert("Ajax Fehlfunktion:" + errorMsg);
        },
        data: {"postId": 1},

        
    });
Posted
Updated 4-Feb-19 4:38am

1 solution

You only get the first email because you're only looking at the first record. You need to loop through the returned array to look at each record in turn.

For example:
JavaScript
success: function(response){
    $.each(response, function(index, item){
        var postId = item.postId;
        var email = item.email;
        var $info = $("<p/>").text("Post id is: " + postId + " email is " + email);
        $("div.main").append($info);
    });
    
    console.log(response);
}
 
Share this answer
 
Comments
Member 14138754 4-Feb-19 12:11pm    
thank you.
the result is perfect :-)

i get now:
Post id is: 1 email is Eliseo@gardner.biz

Post id is: 1 email is Jayne_Kuhic@sydney.com

Post id is: 1 email is Nikita@garfield.biz

Post id is: 1 email is Lew@alysha.tv

Post id is: 1 email is Hayden@althea.biz

Post id is: 2 email is Presley.Mueller@myrl.com

Post id is: 2 email is Dallas@ole.me

Post id is: 2 email is Mallory_Kunze@marie.org

Post id is: 2 email is Meghan_Littel@rene.us

Post id is: 2 email is Carmen_Keeling@caroline.name

Post id is: 3 email is Veronica_Goodwin@timmothy.net
.....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900