javascript 带有 jQ​​uery 的 themoviedb JSON API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14152276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:50:44  来源:igfitidea点击:

themoviedb JSON API with jQuery

javascriptjqueryjsonapijsonp

提问by nickcoxdotme

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.

我正在尝试做一些看起来应该很简单的事情。获取文本字段的输入并通过 API 在 themoviedb.org 数据库中搜索它。

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

我正在使用 jQuery 和支持 JSONP 的 themoviedb.org APIv3。不过,我得到的只是这个回应:

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

{"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
  • 我有正确的 API 密钥
  • 搜索按钮正在提交并正确获取输入的值

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

这是一个jsfiddle,这是关于搜索电影的API 文档。另外,请查看此版本的 API 文档。我认为这与查询参数有关。

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.

真的,我不知道我在用 JSON 做什么,所以我希望这将是一个有代表性的例子,可以帮助我理解它。

回答by PhearOfRayne

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

您只需要正确数量的查询字符串参数。所需的参数是queryapi_key。我在这里找到了这些要求http://docs.themoviedb.apiary.io/#get-%2F3%2Fsearch%2Fmovie。试试这个:

$(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/

工作小提琴:http: //jsfiddle.net/fewds/srdHD/3/

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

注意:由于那很可能是您的真实api 密钥,我建议您申请一个新的!

回答by Robin

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

您使用的 URL 不正确,您的示例生成以下请求:

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

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

但根据 API 文档,它应该是这样的:

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

I have forked and updated your jsfiddle

我已经分叉并更新了你的jsfiddle

回答by CodeisCode

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

需要正确数量的查询参数。所需的参数是 query 和 api_key

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

如果您通过 urlencode 或解码遇到错误,请在urlencode.inurldecode.in检查您的 url