I can not get the JSON from an AJAX call

1

I have the following code in which I make several AJAX calls to receive a JSON from an API, depending on the first call I make other calls, that work correctly, my problem is in the last call, with the JSON that returns some URLs, what happens if I make the AJAX request without more the format it gives me is a concatenation of URLs that I do not use at all. I would like to split the URLs at the end of each one so that later I can use them, I know that it is done with the function split() and that I have to go through them with a loop but I am stuck. The code that I have is the following:

(function(){

    var API_URL = "https://swapi.co/api/people/";

    var apnd = document.getElementById("apnd");
    var search = document.getElementById("input--search");
    var subm = document.getElementById("submit")

    var persons = {};
    persons.birthday;
    persons.name;
    persons.eyesColor;
    persons.hairColor;
    persons.height;
    persons.skin;
    persons.gender;
    persons.origin;
    persons.species;
    persons.films;

    $(subm).on("click", recivePersons);

    $(search).on("keypress", function(event){
        if(event.which == 13){
            $("#apndText").html("Trabajando....");
            recivePersons();
        }
    })

    function recivePersons() {

        $.getJSON({
            url: API_URL + "?search=" + $(search).val()
        }, starWars);

        function starWars(data) {
            $("#apndText").fadeOut();
            console.log(data);
            console.log(data.results[0].birth_year);

            persons.birthday = data.results[0].birth_year; 
            persons.name = data.results[0].name;
            persons.eyesColor = data.results[0].eye_color;
            persons.hairColor = data.results[0].hair_color;
            persons.height = data.results[0].height; 
            persons.skin = data.results[0].skin_color;
            persons.gender = data.results[0].gender;
            persons.origin = data.results[0].homeworld;
            persons.species = data.results[0].species;
            persons.films = data.results[0].films;

            $("#apndDate").html("Birth Year: " + persons.birthday);
            $("#apndName").html("Name: " + persons.name); 
            $("#apndEye").html("Hair Color: " + persons.hairColor);
            $("#apndEye").html("Eyes Color: " + persons.eyesColor);
            $("#apndHeight").html("Height: " + persons.height + "cm"); 
            $("#apndSkin").html("Skin Color: " + persons.skin);
            $("#apndGender").html("Gender: " + persons.gender);

            console.log(persons.origin)

            $.getJSON({url: persons.origin}, function(data){
                console.log(data);
                // PERSONS.NAME ES UN NUEVO ATRIBTO DENTRO DE OTRA LLAMADA, NO CONFUNDIR CON EL NOMBRE DE PERSONAJE
                persons.originPlanet = data.name;
                $("#origin").html("Origin Planet: " + persons.originPlanet);
            })

            $.getJSON({url: persons.species}, function(data){
                console.log(data);
                // OTRA VEZ PERSONS.NAME ES UN NUEVO ATRIBTO DENTRO DE OTRA LLAMADA, NO CONFUNDIR CON EL NOMBRE DE PERSONAJE
                persons.speciesOwn = data.name;
                $("#specie").html("Specie: " + persons.speciesOwn);
            })

            $.getJSON({url: persons.films}, function(data){
                for( var i = 0; persons.films < 12; i++ ){
                    console.log(data[i].split(","));
                }
            })
        }
    }
})();
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Star Wars</title>
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet">
    <link rel="stylesheet" href="static/css/style.css">
</head>
<body>
    <h1>Search all the people of Star Wars</h1>
    <input type="text" class="text" name="search" id="input--search" placeholder=" Search any person of Star Wars...">
    <input type="submit" value="Buscar" class="submit" id="submit">
    <div class="apnd" id="apnd">
        <p id="apndText"></p>
        <p id="apndDate"></p>
        <p id="apndName"></p>
        <p id="apndEye"></p>
        <p id="apndHair"></p>
        <p id="apndHeight"></p>
        <p id="apndSkin"></p>
        <p id="apndGender"></p>
        <p id="origin"></p>
        <p id="specie"></p>
    </div>
    <script src="static/js/jquery.js"></script>
    <script src="static/js/app.js"></script>
</body>
</html>

The code that does not work for me is the one from the last call

The JSON that I receive is the following:

GET link 404 (NOT FOUND)

The API is public and in the movies part I can not pass a character search parameter to return the movies in which each character came out. On the other hand, when I make a query in the characters part if I receive an Array with URLs from the part of movies in which each character came out.

The easiest links to consult with documentation on the APi and that refer to these are:

link link

    
asked by Juan 19.11.2016 в 13:46
source

1 answer

1

Now that I see the API, the problem is that person.films is an array and therefore, when using it as a URL it is invalid because it is not one, but several URLs separated by a comma.

The result that comes from the API is like this:

  "films": [
    "http://swapi.co/api/films/6/",
    "http://swapi.co/api/films/3/",
    "http://swapi.co/api/films/2/",
    "http://swapi.co/api/films/1/"
  ],

If you want to obtain the information of all the films you must do it by iterating on the arrangement BEFORE making the request and in turn, making a request per element of the arrangement; Something like this:

$.each(persons.films, function(filmUrl) {
  $.getJSON({url: filmUrl}, function(data){
     console.log(data);
  });
});

This is like putting for on the outside, but using each of jQuery which is more convenient.

Note that spices also returns an array. It is working because the array contains only one element, but if it received more than one element, you would have the same problem in that part.

I recommend that you browse the API with a Postman tool, which allows you to visualize the results you get and understand how they are structured. the data.

Salu2

    
answered by 19.11.2016 в 14:34