themoviedb JSON API with jQuery

19,822

Solution 1

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!

Solution 2

The URL you use is not correct, your examples generates the following request:

http://api.themoviedb.org/3/search/MOVIENAME?api_key=APIKEY

but according to the API documentation it should look like this:

http://api.themoviedb.org/3/search/movie?api_key=APIKEY&query=MOVIENAME

I have forked and updated your jsfiddle

Share:
19,822

Related videos on Youtube

nickcoxdotme
Author by

nickcoxdotme

Full stack engineer from sunny Seattle, WA.

Updated on September 18, 2022

Comments

  • nickcoxdotme
    nickcoxdotme over 1 year

    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.

  • Anand Jain
    Anand Jain about 8 years
    what is filling in input variable.