Search code examples
jqueryajaxrotten-tomatoes

Error: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access


I am new to AJAX calls and I would like to mock up the rottentomatoes search page. I'm getting the correct JSON file but it won't load on my browser. I have been researching and have not found a solution. Any help would be appreciated.

Here is my code.

$(document).ready(function() {
  var apikey = "qvq7jf4n29fv8m8pxqqyxsxg";
  var baseUrl = "http://api.rottentomatoes.com/api/public/v1.0";
  var moviesSearchUrl = baseUrl + '/movies.json?apikey=' + apikey;

 $('form').submit(function (evt) {
    var $submitButton = $('#submit');
    var $searchField = $('#search');
    evt.preventDefault();
    $searchField.prop("disabled",true);
    $submitButton.attr("disabled", true).val("searching....");
    var query = $searchField.val();
    var rotten = moviesSearchUrl + '&q=' + encodeURI(query);

    $.ajax(rotten,

    function(data){

      if (data.items.length > 0) {
        $.each(data.items,function(i,movie) {
          movieHTML += '<li class="grid-25 tablet-grid-50">';
          movieHTML += '<a href="' + movie.title + '" class="image">';
          movieHTML += '<img src="' + movie.posters.original + '"></a></li>';
        }); // end each
      } else {
        movieHTML = "<p>No photos found that match: " + animal + ".</p>"
      }
      $('#movies').html(movieHTML);
      $searchField.prop("disabled", false);
      $submitButton.attr("disabled", false).val("Search");
    }); // end getJSON

  }); // end click

}); // end ready

Solution

  • You just needed to tweak .ajax to use jsonp.

    Here is working jsfiddle http://jsfiddle.net/hvtz7kfv/2/

     $.ajax({
         dataType: "jsonp",
         url:  rotten,   
         success: searchCallback
    
    });