Load Json file with Ajax

Asked

Viewed 595 times

1

How to upload a Json file to my html. The console does not show any error, but no field is filled with data from the Json file.

<h3 class="center">Tela de alunos</h3>

<div class="container">
    <form class="col s12">
        <div class="row">
            <div class="input-field col s5 offset-s1">
                <input placeholder="Nome do aluno" id="aluno" type="text" class="validate">
                <label for="aluno"></label>
            </div>
            <div class="input-field col s3">
                <input placeholder="Série" id=serie type="text" class="validate">
                <label for="serie"></label>
            </div>  
            <div class="input-field col s1">
                <input placeholder="Classe" id=classe type="text" class="center validate">
                <label for="classe"></label>
            </div>       
        </div>
    </form>
</div>

<div class="container">
    <div class="col s12 l6">
        <div class="row">
            <a class="waves-effect waves-light btn left" onclick="loadDoc()">Carregar</a>
            <a class="waves-effect waves-light btn right">Editar</a>
        </div>
    </div>
</div>

Follow the Javascript

    function loadDoc() {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                var students = JSON.parse(this.responseText);
                var degrees = JSON.parse(this.responseText);
                var classes = JSON.parse(this.responseText);
                    document.getElementById("aluno").value = students[0].name;
                    document.getElementById("serie").value = degrees[0].degreeId;
                    document.getElementById("classe").value = classes[0].classId;
            }
        };
        xmlhttp.open("GET", "students.json", true);
        xmlhttp.send();
    }
  • Where is JSON? is a file or are you going to search on the internet? You can put the code you already have?

  • @Sergio In a file in the same html directory.

1 answer

0


Put the Ids with quotes and tag input you should put .value and not .innerHTML

<div class="container">
    <form class="col s12">
        <div class="row">
            <div class="input-field col s5 offset-s1">
                <input placeholder="Nome do aluno" id="aluno" type="text" class="validate">
                <label for="aluno"></label>
            </div>
            <div class="input-field col s3">
                <input placeholder="Série" id="serie" type="text" class="validate">
                <label for="serie"></label>
            </div>
            <div class="input-field col s1">
                <input placeholder="Classe" id="classe" type="text" class="center validate">
                <label for="classe"></label>
            </div>
        </div>
    </form>
</div>

<div class="container">
    <div class="col s12 l6">
        <div class="row">
            <a class="waves-effect waves-light btn left" onclick="loadDoc()">Carregar</a>
            <a class="waves-effect waves-light btn right">Editar</a>
        </div>
    </div>
</div>

<script>
    function loadDoc() {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                json = JSON.parse(this.responseText);

                document.getElementById("aluno").value = json[0].name;
                document.getElementById("serie").value = json[0].degreeId;
                document.getElementById("classe").value = json[0].classId;    }


        };
        xmlhttp.open("GET", "json.json", true);
        xmlhttp.send();
    }
</script>

JSON:

[  
   {  
      "id":1,
      "ra":12346,
      "name":"Pedro Santos Neves",
      "degreeId":1,
      "classId":1
   },
   {  
      "id":2,
      "ra":456798,
      "name":"Maria Antônia Silva",
      "degreeId":2,
      "classId":1
   },
   {  
      "id":3,
      "ra":752156,
      "name":"José Carlos do Prado",
      "degreeId":3,
      "classId":2
   },
   {  
      "id":4,
      "ra":852348,
      "name":"Aparecido Costa Ribeiro",
      "degreeId":4,
      "classId":2
   },
   {  
      "id":5,
      "ra":454643,
      "name":"Mariana Antunes Aguiar",
      "degreeId":6,
      "classId":2
   }
]
  • A certain guy only who is now returning Undefined in the fields.

  • @Leandro puts Json here, Undefined is error of how you’re pulling the json invormation.

  • I adapted the script for you to have a working example.

  • [&#xA; {&#xA; "id":1,&#xA; "ra":12346,&#xA; "name":"Pedro Santos Neves",&#xA; "degreeId":1,&#xA; "classId":1&#xA; },&#xA; {&#xA; "id":2,&#xA; "ra":456798,&#xA; "name":"Maria Antônia Silva",&#xA; "degreeId":2,&#xA; "classId":1&#xA; },&#xA; {&#xA; "id":3,&#xA; "ra":752156,&#xA; "name":"José Carlos do Prado",&#xA; "degreeId":3,&#xA; "classId":2&#xA; },&#xA; {&#xA; "id":4,&#xA; "ra":852348,&#xA; "name":"Aparecido Costa Ribeiro",&#xA; "degreeId":4,&#xA; "classId":2&#xA; },&#xA; {&#xA; "id":5,&#xA; "ra":454643,&#xA; "name":"Mariana Antunes Aguiar",&#xA; "degreeId":6,&#xA; "classId":2&#xA; }&#xA;]

  • function loadDoc() {&#xA; var xmlhttp = new XMLHttpRequest();&#xA; xmlhttp.onreadystatechange = function() {&#xA; if (this.readyState == 4 && this.status == 200) {&#xA; var students = JSON.parse(this.responseText);&#xA; var degrees = JSON.parse(this.responseText);&#xA; var classes = JSON.parse(this.responseText);&#xA; document.getElementById("aluno").value = students.name;&#xA; document.getElementById("serie").value = degrees.degreeId;&#xA; document.getElementById("classe").value = classes.classId;&#xA; }&#xA; };&#xA; xmlhttp.open("GET", "students.json", true);&#xA; xmlhttp.send();&#xA; }

  • Voce wants when clicking "Load" it to load which of the students?

  • I updated my post with an example using your json. Note that json[0].name takes the name of the first student. If you want the second json[1] and so on.

  • Correct Karl had already succeeded here. Now I have another question, I even asked another question here on the forum. How to list all students, grades (degreeId) and class(classId) in this file.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.