0

我目前正在使用 TMDB API 构建一个 Web 应用程序,当我输入任何电影名称时,它应该向我显示所需电影的海报。我不知道如何搜索所需的电影并获取海报的 URL。链接-> https://developers.themoviedb.org/3/movies/get-movie-imageshttps://developers.themoviedb.org/3/configuration/get-api-configuration

我的代码:

<html>
<head>
    <title>Test app</title>
    <style type="text/css">
        #fetch{
            position: absolute;
            text-align: center;
            left: 450px;
            top: 35px;
        }
    </style>
</head>
<body>

<form name="fetch-movie-title" id="fetch">
     <input type="text" placeholder="Enter a movie title" id="term" />
     <input type="submit" onclick="showdata()" id="search" value="Find me a poster" />
</form>

<script type="text/javascript">

function showdata() {
    var film = document.getElementById('term').value;
    console.log(film);
}

    var requestURL = "https://api.themoviedb.org/3/movie/9340/images?api_key=my-api-key";

    var request = new XMLHttpRequest();

    request.open('GET', requestURL);

    request.responseType = 'json';

    request.send();

    request.onload = function(){
        var myjsondata = request.response; //request.response contains all our JSON data 

        console.log(myjsondata);
        var str = "<img src = 'https://image.tmdb.org/t/p/w500"+ myjsondata.posters[2].file_path +"'/>";
        console.log(str);
        document.write(str);
    } 

</script>
</body>
</html>
4

1 回答 1

1

我无法对其进行测试,因为获取他们的 API 密钥需要太多麻烦,但我想您需要的是搜索调用

<html>
<head>
    <title>Test app</title>
    <style type="text/css">
        #fetch{
            position: absolute;
            text-align: center;
            left: 450px;
            top: 35px;
        }
    </style>
</head>
<body>

<form name="fetch-movie-title" id="fetch">
     <input type="text" placeholder="Enter a movie title" id="term" />
     <input type="submit" onclick="showdata()" id="search" value="Find me a poster" />
</form>

<script type="text/javascript">

function showdata() {
    var film = document.getElementById('term').value;
    console.log(film);

    var requestURL = "https://api.themoviedb.org/3/search/movie?api_key=<<api_key>>&language=en-US&query="+film+"&page=1&include_adult=false";

    var request = new XMLHttpRequest();

    request.open('GET', requestURL);

    request.responseType = 'json';

    request.send();

    request.onload = function(){
        var myjsondata = request.response; //request.response contains all our JSON data 

        console.log(myjsondata);
        var str = "<img src = 'https://image.tmdb.org/t/p/w500"+ myjsondata.results[0].poster_path+"'/>";
        console.log(str);
        document.write(str);
    } 

</script>
</body>
</html>

请参阅此链接-> https://developers.themoviedb.org/3/search/search-movies,然后单击使用您的 api-key 以及搜索查询进行尝试。

于 2017-06-07T14:02:39.967 回答