Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

themoviedb JSON API with jQuery

I'm trying to do something that seems like it should be simple. Get the input of a text field and search the themoviedb.org database for it via the API.

I'm using jQuery and the themoviedb.org APIv3, which supports JSONP. All I get, though, is this response:

{"status_code":6,"status_message":"Invalid id - The pre-requisite id is invalid or not found"}

That doesn't really tell me a lot. ID of what?

Things I know:

  • I have the right API key
  • The search button is submitting and getting the value of the input correctly

Here's a jsfiddle, and here's the API documentation about searching movies. Also, check out this version of the API docs. I think it has to do with the query params.

Really, I have no idea what I'm doing with JSON, so I'm hoping this will be a representative example that will help me understand it.

like image 291
nickcoxdotme Avatar asked Dec 09 '22 18:12

nickcoxdotme


1 Answers

You just need the correct number of query string parameters. The required paramaters are query and api_key. I found these requirements here http://docs.themoviedb.apiary.io/#get-%2F3%2Fsearch%2Fmovie. Give this a try instead:

$(document).ready(function() {
    var url = 'http://api.themoviedb.org/3/',
        mode = 'search/movie?query=',
        input,
        movieName,
        key = '&api_key=470fd2ec8853e25d2f8d86f685d2270e';

    $('button').click(function() {
        var input = $('#movie').val(),
            movieName = encodeURI(input);
        $.ajax({
            type: 'GET',
            url: url + mode + input + key,
            async: false,
            jsonpCallback: 'testing',
            contentType: 'application/json',
            dataType: 'jsonp',
            success: function(json) {
                console.dir(json);
            },
            error: function(e) {
                console.log(e.message);
            }
        });
    });
});​

Working Fiddle: http://jsfiddle.net/fewds/srdHD/3/

Note: Since thats most likely your REAL api key I would suggest requesting a new one!

like image 108
PhearOfRayne Avatar answered Dec 11 '22 06:12

PhearOfRayne