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