Search code examples
javascriptjqueryapiitunes

How to get specific value from iTunes API using Javascript


This is my API call: https://itunes.apple.com/search?term=cut+the+cord&limit=1

I only want to get the value of the '"releaseDate":"2015-06-29' part.

Is there any way that I could only get that part in a textbox. I have this textbox in another page. Any ideas or suggestions?


Solution

  • Due to the cross-domaine policy, you must use JSONP to get json data and then format response value:

    $.ajax({
        url: "http://itunes.apple.com/search?term=cut+the+cord&limit=1",
        dataType: "jsonp",
        success: function( response ) {
            console.log( response );
            var d = new Date(response.results[0].releaseDate);
            var y = d.getFullYear();
            var m = pad( d.getMonth()+1, 2);
            var d = pad( d.getDate(), 2);
            $("#release").attr('value', y + "-" + m + "-" + d)
        }
    });
    

    http://jsfiddle.net/yts728L5/2/