views:

426

answers:

2

I'm trying to grab some JSON from Apple's iTunes JSON service. The request is simple: http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsSearch?term=jac&limit=25

If you visit the URL in your browser you will see some well-formed (backed up by jsonlint.com) JSON. When I use the following jQuery to make the request, however, the request finds nothing:

        $("#soundtrack").keypress(function(){
            $.getJSON("http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsSearch",{'term':$(this).val(), 'limit':'25'}, function(j){              
                var options = '';
                for (var i = 0; i < j.results.length; i++) {
                    options += '<option value="' + j.results[i].trackId + '">' + j.results[i].artistName + ' - ' + j.results[i].trackName + '</option>';
                }
                $("#track_id").html(options);
            });
        });

Firebug sees the request, but only receives an empty response.

Any help would be appreciated here, as I'm at my whits end trying to solve it. You can view the script here: http://rnmtest.co.uk/gd/drives_admin/add_drive (soundtrack input box is at the bottom of the page).

Thanks

+1  A: 

In order to do cross-domain requests, your going to need to use JSONP. This may help:

$.ajax({
  url: "http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsSearch",
  dataType: 'jsonp',
  data: {'term':$(this).val(), 'limit':'25'}, 
  success: function(j){              
    var options = '';
    for (var i = 0; i < j.results.length; i++) {
      options += '<option value="' + j.results[i].trackId + '">' + j.results[i].artistName + ' - ' + j.results[i].trackName + '</option>';
    }
    $("#track_id").html(options);
  }
});
gnarf
That's worked a treat, thanks :)
mikemike
+2  A: 

Or you simply change the url a bit. From

http://ax.phobos.apple.com.edgesuite.net/.../wa/wsSearch"

to

http://ax.phobos.apple.com.edgesuite.net/.../wa/wsSearch?callback=?"

And keep using $.getJSON instead of switching to $.ajax

From the jQuery.getJSON documentation

If the URL includes the string "callback=?" in the URL, the request is treated as JSONP instead.

jitter
+1 although, I usually suggest everyone get used to using `$.ajax`, as `$.get` `$.getJSON` `$.post` etc, all map to the same `$.ajax` function anyway, might as well just learn its parameters rather than the order of each of the other functions too.
gnarf
thx. But I'm sure everybody knows the other functions just use `$.ajax` in the backgroud. But I prefer the improved readability when using the "shorthand"-functions. This way I don't need to scan the whole options object to know what is going on.
jitter