I am unable to display the array data

Asked

Viewed 51 times

1

when I type the console.log(Response) command it displays all the information in the browser console, but for some detail, I’m not able to display the data inside my div which is in the html snippet below. Can someone point out to me what I’m missing?

<script>
    xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        myFunction(xmlhttp.responseText);
      }
    };

    xmlhttp.open("GET", url, true);
    xmlhttp.send();

    myFunction(url);
    function myFunction(response) {
        var arr = JSON.parse(JSON.stringify(response));
        var i;
        var out = "<div>";

        for(i = 0; i < arr.length; i++) {
            out += "<a href='#' class='samba-playlist-trigger list-group-item active' data-mediaid=" + arr[i].id + "></a>";
        }
        out += "</div>";
        document.getElementById("id01").innerHTML = out;
    }
</script>

The display part in the html page

<div class="container">
    <div class="row">
        <div id="id01"></div>
    </div>
</div>
  • What’s the idea of myFunction(url);? and where are you instantiating ajax? Something else, the JSON.stringify doesn’t make sense because you’re getting a string.

1 answer

2


Three things come to mind:

  • What’s the idea of myFunction(url);? that line makes no sense because myFunction is what you want to use to process the right ajax result?
  • missing the code that calls ajax
  • the JSON.stringify doesn’t make sense because you’re getting a string.

Suggestion:

function chamarAjax(url, callback) {
  var request = new XMLHttpRequest();
  request.open('GET', url, true);

  request.onload = function() {
    if (request.status >= 200 && request.status < 400) {
      callback(request.responseText);
    } else {
      console.log('Houve um erro!');
    }
  };

  request.onerror = function() {
    console.log('Houve um erro!');
  };

  request.send();
}

function myFunction(response) {
  var arr = JSON.parse(response);
  var ancoras = arr.map(function(obj) {
    return "<a href='#' class='samba-playlist-trigger list-group-item active' data-mediaid='" + obj.id + "'>Link</a>";
  }).join('');
  document.getElementById("id01").innerHTML = ["<div>", ancoras, "</div>"].join('');
}

chamarAjax('http://sergiofrilans.se/so_pt_stuff/184974.php', myFunction);
a {
  display: block;
}
<div id="id01"></div>

That way you have ajax inside a function and your function to process ajax to be passed as callback, getting everything more organized.

  • Well that I thought it was kind of strange my code, I’m new to the json subject. Sergio, could you enlighten me on why ajax? Thank you.

  • @Vandrépaulo in your code you have xmlhttp, it’s the same as I have, only I named it request. What you lack is var request = new XMLHttpRequest();, that if you want to use your variable name would be var xmlhttp = new XMLHttpRequest();. This is what is called ajax, IE XMLHttpRequest is the same as ajax request.

  • Thanks for the explanation, @Sergio. I’ll make the adjustments and put the result here. Strong hug!

  • @Vandrépaulo of nothing. If there are still doubts say. If the answer solves your problem (or most of it solves) you do not need to put the result here. You can click to accept the answer. Do as you please :)

  • it is not yet displaying in the browser. In the console it shows the Array with the objects perfectly. I just can’t show it on my website, inside my div, it’s like it carries the code part <a href='#' class='samba-playlist-Trigger list-group-item active' data-mediaid=" + obj.id + "></a> emptiness.

  • @Vandrépaulo noticed an error: it lacks simple quotes here: data-mediaid=" + obj.id + ">. Must be data-mediaid='" + obj.id + "'>. If it doesn’t, what does console.log(typeof arr, arr); on the line after var arr = JSON.parse(response);?

  • he shows object [Object, Object, Object, Object, Object] and all contents of the Array

  • @Vandrépaulo ok, so this is not a array... what gives console.log(Object.keys(arr));? Or rather: console.log(JSON.stringify(arr));?

  • now it shows the direct content, what I really want to display: [{"id":"626aa442fb8ffe87ff98c659088843af","title":"O PROSCRITO","status":"ACTIVE","qualifier":"VIDEO","description":"<div>EUA</div><div>1943</div>...

  • @Vandrépaulo this is the result of console.log(JSON.stringify(arr))?

  • @Vandrépaulo edited the answer, now with a working example...

  • that’s right. I want to display the title, the description and other information that it’s pulling showing on the console. I implemented your code, but it still doesn’t display the information. Only on the console

  • @Vandrépaulo makes a jsFiddle that reproduces your problem for me to see. Use the url I put in the answer if you want.

  • https://jsfiddle.net/Lgkccuxf/

  • @Vandrépaulo works as is: http://jsfiddle.net/Lgkccuxf/ (note that without HTTPS because my website only responds to HTTP).

Show 10 more comments

Browser other questions tagged

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