Peter
Peter

Reputation: 11835

Jquery Post - Itunes API returns 200 and error

the json response is valid
http://itunes.apple.com/search?term=jack+johnson

but i get an error ... why?

Example: http://jsfiddle.net/36Vxs/

js

$(document).ready(function() {
var jqxhr = $.ajax( "http://itunes.apple.com/search?term=jack+johnson" )
    .done(function(data) { console.log(data); })
    .fail(function(data) { console.log(data); })
 });

Thanks in advance!

Upvotes: 1

Views: 2005

Answers (2)

joevallender
joevallender

Reputation: 4293

Thanks for posting the documentation link, it tells you to use JSONP - see here http://jsfiddle.net/joevallender/rMKZw/2/

$(document).ready(function() {
  $.getJSON(
      'http://itunes.apple.com/search?term=jack+johnson&callback=?', 
      function ( data ) {
         console.log(data)

     });
 });

It doesn't jump out at you through

Note: When creating search fields and scripts for your website, you should use dynamic script tags for your xmlhttp script call requests. For example:

<script src="http://.../search?parameterkeyvalue&callback="{name of JavaScript function in webpage}"/>

If you see something like that, you need to both use $.getJSON and add callback=? to your request in jQuery to invoke JSONP

Upvotes: 0

starskythehutch
starskythehutch

Reputation: 3338

You can't get JSON from another domain. You need to get JSONP. The following works:

$(document).ready(function() {
    $.ajax({
        url: "http://itunes.apple.com/search?term=jack+johnson",
        dataType: 'JSONP'
    })
    .done(function(data) { console.log(data); })
    .fail(function(data) { console.log(data); })
 });

Upvotes: 6

Related Questions