使用XMLHttpRequest为TMDB构建图像URL?

时间:2017-06-06 10:54:36

标签: javascript api themoviedb-api

如何构建网址,然后使用javascript获取电影的特定海报。我在这里浏览了TMDB的文档 - > https://developers.themoviedb.org/3/movies/get-movie-imageshttps://developers.themoviedb.org/3/configuration/get-api-configuration但由于我是API新手,因此文档非常混乱。基本上我试图用纯Javascript复制这个 - > https://www.smashingmagazine.com/2012/02/beginners-guide-jquery-based-json-api-clients/

<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>

0 个答案:

没有答案