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?

8
  • 2
    What's the length of the array ['a', 'b']? What is the highest index in the array? Commented Jul 5, 2013 at 17:44
  • The the feed returns varied lengths Commented Jul 5, 2013 at 17:51
  • @JodyHeavener Felix means there is no item in array with index equal to array length Commented Jul 5, 2013 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? Commented Jul 5, 2013 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. Commented Jul 5, 2013 at 17:56

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

12 Comments

Ah yes. Still no luck to that extent though!
@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.
It doesnt look like this api supports jsonp.
if you set a callback response should come in a wrapper if it has to suuport jsonp
Boom. It works! Now I guess the only problem is the heavy request usage on both my server and the external server.
|
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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.