1

I am trying to access this JSON feed to get the last item in the feed which is the current song playing on the radio station, so I can push it in to a stream player web app:

function getSongData(){
    $.ajax({
        url: 'http://cjzn.streamon.fm/metadata/recentevents/CJZN-48k.json',
        dataType: 'jsonp',
        success: function(data) {
            var totalItems = data.length;
            alert('Current song: ' + data[totalItems].TIT2);
        },
        error: function() {
                alert('Unable to retrieve data');
        }
    });
}

As you can see I'm using data.length to retrieve the total number of items, which would mean that the number is also the last item in the feed. All I can get is the error message! I understand I need to use JSONP to get the data to my domain, but I honestly don't know if JSONP is supported do that could be the culprit. If so, how else do I get the feed data?

Jody Heavener
  • 2,704
  • 5
  • 41
  • 70
  • 2
    What's the length of the array `['a', 'b']`? What is the highest index in the array? – Felix Kling Jul 05 '13 at 17:44
  • The the feed returns varied lengths – Jody Heavener Jul 05 '13 at 17:51
  • @JodyHeavener Felix means there is no item in array with index equal to array length – A. Wolff Jul 05 '13 at 17:52
  • Ah gotcha. I was using that under the impression that `length` returned the total number of items. How do I get total number? – Jody Heavener Jul 05 '13 at 17:55
  • `length` *does* return the total number of items. `['a', 'b']` contains two elements, hence `.length` returns `2`. The first element has the index `0`, the second (and last) element has the index `1`. – Felix Kling Jul 05 '13 at 17:56
  • Ya, length returns the total items for an array BUT the highest index of an array is less than its length (length minus one) – A. Wolff Jul 05 '13 at 17:56
  • Right, and so I tried doing `data[totalItems-1].TIT2` to no avail - as commented below it seems as though this feed doesn't support jsonp, so I should look for an alternative to retrieving the data. Guess it's time for me to do some more reading! – Jody Heavener Jul 05 '13 at 18:02
  • @JodyHeavener Your url doesn't handle jsonp callbacks. See the update – PSL Jul 05 '13 at 18:04

2 Answers2

5

You should.

change

data[totalItems].TIT2

to

data[totalItems-1].TIT2

Array indexes starts with 0. so you need to do length-1 for the last item. data[totalItems] will go out of bounds and you will get an error as you access undefined.TIT2 effectively.

This api doesnt seem to support JSONP. if you fire the ajax with jquery, it appends a callback string to the url for jsonp callback.

ex: http://cjzn.streamon.fm/metadata/recentevents/CJZN-48k.json?callback=jQuery19105709207213949412_1373047199605&_=1373047199606

But the response doesnt get wrapped in the function with the callback name, ideally the response should be:

jQuery19105709207213949412_1373047199605(jsonData);

But it doesn't, instead it just gives the json response as is.

PSL
  • 123,204
  • 21
  • 253
  • 243
  • Ah yes. Still no luck to that extent though! – Jody Heavener Jul 05 '13 at 17:49
  • @Jody: It seems that the URL does not return JSONP. You cannot use JSONP if the server does not support it. You have to proxy the request through your server. – Felix Kling Jul 05 '13 at 17:53
  • It doesnt look like this api supports jsonp. – PSL Jul 05 '13 at 17:53
  • if you set a callback response should come in a wrapper if it has to suuport jsonp – PSL Jul 05 '13 at 17:54
  • Any suggestions on how else to retrieve this data? I'm not entirely familiar with it, but I imagine I could download the feed to my server every few minutes (ughhh) using PHP and handle it locally from there? – Jody Heavener Jul 05 '13 at 18:06
  • @JodyHeavener You probably cannot. There is no way unless they support JSONP or you are in the same domain as that of the target url which visibly you are not. – PSL Jul 05 '13 at 18:07
  • So essentially I can't regularly retrieve the data at this given point? – Jody Heavener Jul 05 '13 at 18:08
  • @JodyHeavener No because if you avoid jsonP the call will not event go through because you are in a different domain. You should be able to see `access control allow origin` error in your console. Its all for security measure, but it seems like they have not implemented CORS properly. – PSL Jul 05 '13 at 18:10
  • Using PHP I ran `file_put_contents("playlist.json", fopen("http://cjzn.streamon.fm/metadata/recentevents/CJZN-48k.json", 'r'));` and now the file **playlist.json** is now on my server (http://lab.starchcreative.co/zapp/playlist.json). Can't I just regularly download the external feed to my server and then pull the data from my local copy? – Jody Heavener Jul 05 '13 at 18:16
  • @JodyHeavener yes once the file is in your domain. You can do it without using jsonp. – PSL Jul 05 '13 at 18:17
  • 1
    Boom. It works! Now I guess the only problem is the heavy request usage on both my server and the external server. – Jody Heavener Jul 05 '13 at 18:19
  • function getSongData(){ $.ajax({ url: 'http://cjzn.streamon.fm/metadata/recentevents/CJZN-48k.json', dataType: 'jsonp', success: function(data) { var totalItems = data.length; alert('Current song: ' + data[totalItems-1].TIT2); }, error: function() { alert('Unable to retrieve data'); } }); } – Avi Sep 20 '15 at 17:37
1

As mentioned in the comments this feed didn't support JSONP. So instead of pulling directly from the StreamON feed I did the following to write that feed data to my own server and effectively copy the feed:

file_put_contents("playlist.json", fopen("http://cjzn.streamon.fm/metadata/recentevents/CJZN-48k.json", 'r'));

So now playlist.json is located on my server and I can pull from that using JSON. I just have to regularly write the external feed to my server.

A hat tip also to the commenters about my problem with retrieving the last item in the feed and forgetting to subtract 1 as the index starts at 0 and not 1. Thanks guys!

Jody Heavener
  • 2,704
  • 5
  • 41
  • 70