0

Im trying to use this api that gives me a json. But I can't display any info. I got this code in Jquery:

$(document).ready(function(){
  $("#search").click(function(){
      var title = $("#words").val().replace(/\s/g,"+");
      var url = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q="+title+"&apikey=ng6gbx7vdpwmyfwd7vp5g799" + "?callback=?";

   $.getJSON(url, null, function(data){           
           alert(data.total);

   });
  });
});

Why? I have no idea.

Beemo
  • 17
  • 1
  • 5

1 Answers1

0

The API only supports JSONp. so you need to do the following

$(document).ready(function(){
    $("#search").click(function(){
        var title = $("#words").val().replace(/\s/g,"+");
        var url = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q="+title+"&apikey=ng6gbx7vdpwmyfwd7vp5g799";

         $.ajax(url, {
             dataType: "jsonp",
             success: function(data){           
                 alert(data.total);
             }  
         });
    });
});

See this fiddle: http://jsfiddle.net/rpTm3/

updated fiddle with alert: http://jsfiddle.net/rpTm3/1/

Michael Coxon
  • 5,311
  • 1
  • 24
  • 51
  • It's because browser can download JSON only from your domain. You need jsonp workaround to download json files from other servers. Check [this](http://en.wikipedia.org/wiki/JSONP) – Andrey Korchak Feb 20 '14 at 12:46
  • Also cause they don't have a `Access-Control-Allow-Headers` tag in the headers. – Michael Coxon Feb 20 '14 at 12:48