0

It's my first question here so please bear with me. I'm coding a piece of a web app that gets the content of another webpage with $.get and then assigns it to an array. The returned content of the web page is the typical JS array (The following is literally the return of the $.get):

[{"id":2476,"name":"XXXX","start_date":"2018-06-06 00:00:00","end_date":"2018-06-06 23:59:59","current_attendees":2442}]

The thing is that it seems to be assigning it as a string (An alert shows the whole string just like the one above) and a later events.length says it is undefined. Bottom line, how can I make Javascript recognize that it is an array or not a string? Thanks. The code:

<script>
  function upcomingevents(){
    var events, id, place, date;
    id = <?php if($spotlightevent != null ){echo $spotlightevent;}else{echo 0;} ?>;
    maineventplace = 0;
    $.get("THERE IS A LINK HERE", function(data){
        events=data;
        alert(events);
    }, "HTML");
    if( id != null){
      for(i = 0; i = events.length; i++){ //Throws "Can't get length of undefined"
        if(events[i]["id"] == id){
          $("#maineventtitle").text(events[i]["name"]);
          $("#maineventcount").text("Inscritos: " + events[i]["current_attendees"]);
          $("#maineventsponsor").text("Valor Patrocínios: " + <?php echo $spotlighteventsponsor; ?> + "€" );
          maineventplace = i;
        }
      }
    }
    for(i = 0; i = events.length; i++){
      if(events[i]["id"] != id && i != maineventplace){
        $("#secondeventtitle").text(events[i]["name"]);
        $("#secondneventcount").text("Inscritos: " + events[i]["current_attendees"]);
      }
    }
  }
  setInterval(upcomingevents(), 30000);
</script>
  • The string you gave in your first snippet is json. You need to parse it with `JSON.parse(theString)` However you have other issues that can be addressed in a duplicate post. – Taplar May 18 '18 at 15:39
  • After you solve the async issue the next problem is the last line which should be `setInterval(upcomingevents, 30000);` – JJJ May 18 '18 at 15:42

0 Answers0