Search code examples
javascriptjqueryimdb

How to search for a whole string using IMDB api?


IMDB API says to use the following but if i search for "The wolf of wall street" it will search by separating the words and not by the whole string.

HTML

<form name="search-imdb" autocomplete="off">
  <input type="hidden" value="483aa4e4-59f8-4728-804d-8f4bb4311d5a" name="api_key">
   <div class="row">
    <div class="col-md-8">
      <input class="form-control" required="" type="text" name="q" title="Type Name, Title, Character, etc. and hit Enter" placeholder="Type Name, Title, Character, etc. and hit Enter">
    </div>
    <div class="col-md-4">
      <button class="btn btn-primary btn-block">Search IMDb</button>
    </div>
   </div>
  <div class="code"></div>
 </form>

JS

$('form[name="search-imdb"]').on("submit", function(e) {
  var form = $(this);
  e.preventDefault();
  $.ajax({
     url: "http://imdb.wemakesites.net/api/search",
     data: form.serialize(), // assuming the form has a hidden input with api_key name, containing your API key
     crossDomain: true,
     dataType: "jsonp",
     success: function(data) {
       window.console.log(data);
     }
  });
});

JSON

"data": {
        "results": {
            "titles": [
                {
                    "title": "The...",

Solution

  • The demo page neglects encode the search term and instead leaves literal spaces in the url find?q=wolf of wall street. The API most likely interprets as find?q=wolf because spaces are considered invalid and the rest of the URL is ignored.

    This works: http://www.imdb.com/find?q=wolf%20of%20wall%20street

    To URL encode your search query encodeURIComponent():

    var term = 'wolf of wall street';
    var encodedTerm = encodeURIComponent(term);
    

    URL encoding for exact search terms like this is usually preferable to using + for spaces because it will encode other special characters like accents and punctuation that might be in a movie title, including plus signs +.