- Previous: Response Format
- Up: Introduction
- Next: Branding Guidelines
Code Samples
Using the Code Samples
We're creating some code samples to help get you started using the eMusic API. As time goes on we'll be adding more examples in different languages.
EXAMPLE 1: ALBUM SEARCH WITH JQUERY AJAX AND JSON
This basic example searches for a particular album name and uses the result to display the artist name, album name, and an album image linked to the relevant eMusic page. It uses only front-end technologies to access the API. Note that due to browser security restrictions, it is not possible access a remote server through an Ajax call unless the JSONP response format is used, as we have here. If you want to receive XML directly in the browser through an API, you'll need to have a server-side script or application handle the API call.
You'll need to grab the jQuery library as well as the eMusic branding image, to reference them locally. Be sure to add your API key in as well. <html>
<head>
<title>eMusic API example</title>
<style type="text/css">
body {
background: #ccc;
margin:20;
font: 16px "Helvetica Neue", Helvetica, "Arial Unicode MS", Arial, sans-serif;
}
</style>
<script type="text/javascript" src="jquery-1.4.1.min.js"></script>
<script type="text/javascript">
$(document).ready(
function() {
$.ajax( {
url: "http://api.emusic.com/album/search",
data: {
perPage: 1,
format: "JSONP",
term: "electric+version",
apiKey: "YOUR_API_KEY_HERE"
},
dataType: "jsonp",
success: function(data) {
var album = data.albums[0];
$('#albumImage').attr("src", album.image);
$('#artistName').html(album.artist.name);
$('#albumName').html(album.name);
$('a#imageLink').attr("href", album.url);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest + ":" + textStatus + ":" + errorThrown);
}
});
}
);
</script>
</head>
<body>
<div id="albumArt"><a id="imageLink"><img id="albumImage" border="0"></a></div>
<div id="artistName"></div>
<div id="albumName"></div>
<div style="margin-top:20px" id="powered"><img id="poweredImage" src="poweredbyemusic_100x38_b.png"></div>
</body>
</html>
We'd love to add an example if you have one of your own. Please contact us if you'd like to have your code considered for this page.
- Previous: Response Format
- Up: Introduction
- Next: Branding Guidelines
Docs Navigation
- Introduction
- Request Format
- Response Elements
- Result Codes
- Response Format
- Code Samples
- Branding Guidelines
- Method Index
- Request Parameters
- Method: Album Charts
- Method: Album Editorial
- Method: Album Info
- Method: Album Picks
- Method: Album Ratings
- Method: Album Reviews
- Method: Album Search
- Method: Artist Charts
- Method: Artist Editorial
- Method: Artist Info
- Method: Artist Picks
- Method: Artist Ratings
- Method: Artist Related
- Method: Artist Search
- Method: Book Charts
- Method: Book Editorial
- Method: Book Info
- Method: Book Ratings
- Method: Book Reviews
- Method: Book Search
- Method: Label Info
- Method: Label Search
- Method: Track Charts
- Method: Track Search
0 Comments
Please sign in to post a comment.