如何构建一个 URL,然后使用 javascript 获取特定的电影海报。我在这里浏览了 TMDB 的文档-> https://developers.themoviedb.org/3/movies/get-movie-images和 https://developers.themoviedb.org/3/configuration/get-api-configuration但作为我是 API 的新手,文档太混乱了。基本上我试图复制这个 - > https://www.smashingmagazine.com/2012/02/beginners-guide-jquery-based-json-api-clients/用纯Javascript。
<body>
<img id="movie-poster" src="">
<input id="movie_title" type="text" name="Enter Movie title" value="Please Enter">
<input type="submit" name="Submit">
<script type="text/javascript">
var getposter = function () {
var film = document.getElementById("movie_title").value;
if(film == ''){
document.write("<h2> We haven't forgotten to validate the form! Please enter something. </h2>")
} else {
document.write("Loading your poster <br>");
var data = "{}";
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("movie-poster", function () {
if (this.readyState === this.DONE) {
console.log(this.responseText);
}
});
xhr.open("GET", "https://api.themoviedb.org/3/movie/%7Bmovie_id%7D/images?language=en-US&api_key=%3C%3Capi_key%3E%3E");
xhr.send(data);
}
}
</script>