fatSlave
fatSlave

Reputation: 84

fetching a json from bandcamp api

sorry if thats been asked earlier, i tried to find but dint get through to start: http://bandcamp.com/developer

i am trying to fetch the bandID of an artist

var my_url = 'http://api.bandcamp.com/api/band/3/search?key=snaefellsjokull&name=metallica';

$.ajax({
                'url' : my_url,
                'type':"GET",
                success: function(res){
                    var bandId = res.result[0].bandId;
                    console.log(bandId);
                }
});

but i am always getting a 405 error that this method is not allowed.

I tried these as well:

$.getJSON(my_url, function(res){
                    var bandId = res.result[0].bandId;
                    findDisco(bandId);
                })

and

 var abc= $.parsejson(my_url);
 abc.res.result[0].bandId;

if u execute the url in navigation bar, it fetches the json .. and i verified it with json lint .. i think i am doing a very silly mistake .. can someone help?

thanks in advance!!

Upvotes: 1

Views: 2309

Answers (1)

Felix Kling
Felix Kling

Reputation: 816830

First, you need to use JSONP (because you are fetching from a different domain, as I explained in my comment), which is supported:

Parameters. There are three parameters that apply to every function call:

key – Your developer key must be included in every call.
debug – Optional parameter that instructs the API to return the text of the JSON blob in a more human-readable format, including spaces and newlines.
callback – If you include this parameter the response will use the JSONP format with the specified callback function name.

Second, you need to access the right fields. E.g. it is res.results instead of res.result and band_id instead of bandId:

Response

For a single band id the response is a hash with the following items. For batch mode, the response is a hash mapping the requested band ids to their blobs of info.

band_id – the numeric id of the band.
name – the band’s name. This may not be unique, especially if the band is shy about their name.
subdomain – the band’s subdomain. This will be unique across all the bands.
url – the band’s home page.
offsite_url – the band’s alternate home page, not on Bandcamp.

All taken from the documentation you linked to.

This works:

var my_url = 'http://api.bandcamp.com/api/band/3/search?key=snaefellsjokull&name=metallica&callback=?';

$.ajax({
    'url': my_url,
    'type': "GET",
    'dataType': 'jsonp',
    success: function(res) {
        var bandId = res.results[0].band_id;
        console.log(bandId);
    }
});

Upvotes: 1

Related Questions