samedi 10 mars 2018

How can I parse a remote JSON file into a JS variable then publish it to an element?

Here's the issue I am having currently. I am trying to take the description data to identify DJs from a JSON-formatted file that automatically updates on my icecast2 server and push the parsed data to a javascript variable, then push it directly to the part of the page. Here is my JS+PHP approach (yes, php is enabled)

    <script language="javascript" type="text/javascript">
function updateDJ() {
var curDJ = "<?php
$json = file_get_contents('http://radio.dredkatt.net:16194/status-json.xsl');
$ar = json_decode($json);
echo $ar->icestats->source->server_description;
?>";
document.getElementById("currentDJ").innerHTML = curDJ;
setTimeout(updateDJ, 10000);
};
updateDJ();
    </script>

The odd thing is that if I were to use an alert(curDJ); to test the variable, it is indeed working, though not when I try to push the variable to the element.

<div id="currentDJ">Unknown DJ</div>

I've also tried a JQuery approach with no luck.

$.getJSON("http://radio.dredkatt.net:16194/status-json.xsl", function(data) {
    $("#currentDJ").text(data.icestats.source.server_description);
}

Been scratching my head over this for awhile.

Aucun commentaire:

Enregistrer un commentaire