سؤال

Hello I am having this issue which I cant seem to figure out were I am getting the latest films from BBC and I am displaying them after that I am fetching each of the films titles and then using the movie db .org api to get the rating of the movie. But the problem here is when I am using the getJson function for the second time as it wont let me input the array as one of the json queries. Here is my code below:

$(function(){
var filmnamevar = [];
    $.getJSON('http://www.bbc.co.uk/tv/programmes/formats/films/player/episodes.json?limit=3',function(data){
        console.log('success');
        $.each(data.episodes,function(i,emp){
            $('#epis').append('<div class="episode-l">'+emp.programme+'<div id="epi-img"><img src="http://ichef.bbci.co.uk/images/ic/192x108/legacy/episode/'+emp.programme.pid+'.jpg"/></div> <div class="ep-title">'+emp.programme.title+'</div><div class="ep-title">'+emp.programme.short_synopsis+'</div></div>');

        console.log(emp);
        filmnamevar.push(emp.programme.title);
        console.log(filmnamevar);
        });

    });

    $.getJSON('http://api.themoviedb.org/3/search/movie?query='+filmnamevar[whichever number]+'&api_key=81c50c197b83129dd4fc387ca6c8c323&limit=1',function(data2){
        $(data2.results,function(i,rat){
            $('#epis').append('<div class="rating">'+rat.vote_average+'</div>');
        });
    });

    });
هل كانت مفيدة؟

المحلول

You're appending the titles to an array (filmnamevar) but then you're accessing filmnamevar directly as if it were a string.

You'll need to loop the array, calling a new getJSON request for each title (unless the movie db API let's you pull many at once):

for( var i = 0; i < filmnamevar.length; i++){
  $.getJSON('http://api.themoviedb.org/3/search/movie?query='+filmnamevar[i]+'&api_key=81c50c197b83129dd4fc387ca6c8c323&limit=1',function(data2){
     $(data2.results,function(i,rat){
       $('#epis').append('<div class="rating">'+rat.vote_average+'</div>');
     });
  });
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top