简体   繁体   中英

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.

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!

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

Need the correct number of query parameters. The required paramaters are query and api_key

If you are getting error on this by urlencode or decodeing please check your url at urlencode.in or urldecode.in

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM