views:

350

answers:

3

Hi I'm looking at the following API http://wiki.github.com/soundcloud/api/oembed-api

the example they give

Call:

http://soundcloud.com/oembed?url=http%3A//soundcloud.com/forss/flickermood&format=json

Response:

{
"html":"<object height=\"81\" ... ",
"user":"Forss",
"permalink":"http:\/\/soundcloud.com\/forss\/flickermood",
"title":"Flickermood",
"type":"rich",
"provider_url":"http:\/\/soundcloud.com",
"description":"From the Soulhack album...",
"version":1.0,
"user_permalink_url":"http:\/\/soundcloud.com\/forss",
"height":81,
"provider_name":"Soundcloud",
"width":0

}

What do i have to do to get this json object from just a url?!!?

+2  A: 

Because the URL isn't on the same domain as your website, you need to use JSONP.

For example: (In jQuery):

$.getJSON(
    'http://soundcloud.com/oembed?url=http%3A//soundcloud.com/forss/flickermood&amp;format=js&amp;callback=?', 
    function(data) { ... }
);

This works by creating a <script> tag like this one:

<script src="http://soundcloud.com/oembed?url=http%3A//soundcloud.com/forss/flickermood&amp;format=js&amp;callback=someFunction" type="text/javascript"></script>

Their server then emits Javascript that calls someFunction with the data to retrieve.
`someFunction is an internal callback generated by jQuery that then calls your callback.

SLaks
cool i'll give it a try - thanks!
Haroldo
@SLaks isn't it necessary to add "?callback=" somewhere in the URL? Otherwise, won't getJSON just do an ordinary XMLHttpRequest GET?
Pointy
@Pointy: You're right; I forgot.
SLaks
this isn't working: <script type="text/javascript"> $(function(){ $.getJSON('http://soundcloud.com/oembed?url=http%3A//soundcloud.com/forss/flickermood alert(e); }); }) </script>
Haroldo
SLaks
still no joy,in the firebug console the GET request is red with a red cross (fail)
Haroldo
+1  A: 

You make a bog standard HTTP GET Request. You get a bog standard HTTP Response with an application/json content type and a JSON document as the body. You then parse this.

Since you have tagged this 'JavaScript' (I assume you mean "from a web page in a browser"), and I assume this is a third party service, you're stuck. You can't fetch data from remote URI in JavaScript unless explicit workarounds (such as JSONP) are put in place.

Oh wait, reading the documentation you linked to - JSONP is available, but you must say 'js' not 'json' and specify a callback: format=js&callback=foo

Then you can just define the callback function:

function foo(myData) { 
    // do stuff with myData
}

And then load the data:

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = theUrlForTheApi;
document.body.appendChild(script);
David Dorward
there is a JSONP option (luckily!)
Haroldo
+1  A: 

It seems they offer a js option for the format parameter, which will return JSONP. You can retrieve JSONP like so:

function getJSONP(url, success) {

    var ud = '_' + +new Date,
        script = document.createElement('script'),
        head = document.getElementsByTagName('head')[0] 
               || document.documentElement;

    window[ud] = function(data) {
        head.removeChild(script);
        success && success(data);
    };

    script.src = url.replace('callback=?', 'callback=' + ud);
    head.appendChild(script);

}

getJSONP('http://soundcloud.com/oembed?url=http%3A//soundcloud.com/forss/flickermood&amp;format=js&amp;callback=?', function(data){
    console.log(data);
});  
J-P
this works perfectly, thank you J-P!interesting that the jquery methods didn't seem to work...
Haroldo