0

I know there are a lot of these questions about this one, but unfortunately i cant find the right answer so i try it again.

I simply want to display the current song from "http://178.19.116.253/currentsong?sid=1" on my site that must refresh every x seconds. I rather not use php cause it will make a background process for all users, so i've seen a lot of little scripts like this..

<div id="sc_stats"><?php include_once 'sc_stats.php'; ?></div>    
var refreshId = setInterval(function() {
$.get("sc_stats.php", function(data) {
    $("#sc_stats").html(data);
});

}, 10000);

But i cant get mine to work without php. Someone can help me with this?

Guillaume
  • 14,306
  • 3
  • 43
  • 40
Jannes
  • 1
  • 5

1 Answers1

1

You don't need to use php, the following snippet which only uses jQuery will display the current song and refresh it every second.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <div class="nowplaying"></div>
    <script src="https://cdn.jsdelivr.net/jquery/3.2.0/jquery.min.js"></script>
    <script>

        setInterval(function() {
            $.ajax({
                url: "http://178.19.116.253/currentsong?sid=1",
                dataType: "html",
                success: function(data) {
                    $('.nowplaying').text(data);
                    console.log(data);
                }
            }); 
        }, 1000);   

    </script>
</body>
</html>

Note: In order to get this to work, either:

  • This piece of code must run on the same domain/server as your stream
  • Or CORS must be enabled on your server. More about CORS can be found here

You can test CORS with the following Chrome plugin: Allow-Control-Allow-Origin:

Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85