I am very lost with this project and I need a guide.
I'm filling a list with characters whose information comes from an Api, these are 10, within this information is the link of the movies in which they appear, my idea is to click on each of these characters and I see the Information (Text, description and image) of the movie in which it appears.
Here is my code with two Ajax calls, so when I click on a character it shows me information about the movie, but it tells me this error
caught SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at peliculas (four.js:43)
at HTMLAnchorElement.onclick (VM22962 index_3.html:1)
being my error in
films=JSON.parse(xmlhttp.responseText);
I leave my complete code
function personajes(url, callback) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.status == 200) {
try {
var data = JSON.parse(xmlhttp.responseText);
} catch(err) {
return;
}
callback(data);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
personajes("https://swapi.co/api/people/", function(data) {
var persons=data.results;
var html ;
var container = document.querySelector(".text ul");
for(var i = 0; i < persons.length; i++){
var item = container.innerHTML += "<li><a class='ajax_item' onClick= 'peliculas()' href='#' > "+ persons[i].name + "</a></li>";
// var item = document.querySelectorAll(".ajax_item").setAttribute('films', JSON.stringify(el.films));
//item.setAttribute('films', JSON.stringify(data.films));
}
})
function peliculas()
{
var xmlhttp = new XMLHttpRequest();
films=JSON.parse(xmlhttp.responseText);
var list=document.querySelector('#results');
list.innerHTML= "";
films.forEach(function(url)
{
//console.log(43, url);
var xmlhttp= new XMLHttpRequest();
xmlhttp.onreadystatechange= function()
{
if (xmlhttp.readyState ===4)
{
if (xmlhttp.status === 200){
film= JSON.parse(xmlhttp.responseText);
//console.log(film.title);
li=document.createElement("li");
li.innerHTML=film.title;
//li.innerHTML=film.opening_crawl;
list.appendChild(li);
}
}
}
xmlhttp.open('GET', url);
xmlhttp.send();
})
}
This is my html
</ul>
</div>
<div class="results">
<ul>
</ul>
</div>
I'm very lost and I do not know how to do it.