0

Im trying to load person data from api : www.swapi.co . However i dont know how can i load movies titles instead of their api adress.

$(function(){

var list = $('.list');
var submit = $('.submit-people');
var people = '1';

submit.on('click',function(){
    people++;
    list.empty();

    $.ajax({

        type: 'GET',
        url: 'https://swapi.co/api/people/' + people + '/',

        success: function(data) {

            list.append('<li>Name:' + data.name + '</li>');
            list.append('<li>Height:' + data.height + '</li>');
            list.append('<li>Mass:' + data.mass + '</li>');
            list.append('<li>Gender:' + data.gender + '</li>');
            list.append('<li>Movies:' + data.films + '</li>');

        }

    })

})

my current code is here : https://jsfiddle.net/wtpy71d7/2/

really thanks for any help or suggestions

1
  • For each of the films in data.films, you have to do another query (another $.ajax) to fetch the details (e.g., movie title). This is a common feature of REST APIs. Commented Jul 25, 2017 at 16:31

2 Answers 2

1

This might help you.

success: function(data) {
     ...
     for(var i = 0; i< data.films.length; i++ )
          $.ajax({
             type: "get",
             url: data.films[i],

             success: function (data) {
                list.append('<li>Movies:' + data.title + '</li>');
             }
          });
     }
}
Sign up to request clarification or add additional context in comments.

Comments

0

It looks like they are giving you the URL for another API Request. What you could do is make another AJAX call and get each movie details, then parse those results and append them to your list. It could look something like this.

list.append('<li>Movies:' + getMovies(data.films) + '</li>');

Your next ajax call might look something like this:

var getMovies = function(moviesAPI) {

$.ajax({
    type: 'GET',
    url: moviesAPI,
  success: function(data) {
  return data;
  }
})

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.