【问题标题】:API and fetching IMDB alternative movie databaseAPI 和获取 IMDB 替代电影数据库
【发布时间】:2021-07-29 06:31:07
【问题描述】:

我正在使用这个 API - https://rapidapi.com/rapidapi/api/movie-database-imdb-alternative 我正在使用 JavaScript 实现,但看不到我应该看到的值。这不是我第一次使用 API,但我不理解这种行为。

我的代码:

fetch("https://movie-database-imdb-alternative.p.rapidapi.com/?i=tt4154796&r=json", {
    "method": "GET",
    "headers": {
        "x-rapidapi-key": "my x-rapidapi-key (don't want to show)",
        "x-rapidapi-host": "movie-database-imdb-alternative.p.rapidapi.com"
    }
})
    .then(response => {
        console.log(response);
        console.log(response.url);
    })
    .catch(err => {
        console.error(err);
    });

我应该得到的回应:

{
"Title":"Avengers: Endgame"
"Year":"2019"
"Rated":"PG-13"
"Released":"26 Apr 2019"
"Runtime":"181 min"
"Genre":"Action, Adventure, Drama, Sci-Fi"
"Director":"Anthony Russo, Joe Russo"
"Writer":"Christopher Markus (screenplay by), Stephen McFeely (screenplay by), Stan Lee (based on the Marvel comics by), Jack Kirby (based on the Marvel comics by), Joe Simon (Captain America created by), Jack Kirby (Captain America created by), Steve Englehart (Star-Lord created by), Steve Gan (Star-Lord created by), Bill Mantlo (Rocket Raccoon created by), Keith Giffen (Rocket Raccoon created by), Jim Starlin (Thanos, Gamora & Drax created by), Stan Lee (Groot created by), Larry Lieber (Groot created by), Jack Kirby (Groot created by), Steve Englehart (Mantis created by), Don Heck (Mantis created by)"
"Actors":"Robert Downey Jr., Chris Evans, Mark Ruffalo, Chris Hemsworth"
"Plot":"After the devastating events of Avengers: Infinity War (2018), the universe is in ruins. With the help of remaining allies, the Avengers assemble once more in order to reverse Thanos' actions and restore balance to the universe."
"Language":"English, Japanese, Xhosa, German"
"Country":"USA"
"Awards":"Nominated for 1 Oscar. Another 69 wins & 109 nominations."
"Poster":"https://m.media-amazon.com/images/M/MV5BMTc5MDE2ODcwNV5BMl5BanBnXkFtZTgwMzI2NzQ2NzM@._V1_SX300.jpg"
"Ratings":[...]3 items
"Metascore":"78"
"imdbRating":"8.4"
"imdbVotes":"856,408"
"imdbID":"tt4154796"
"Type":"movie"
"DVD":"30 Jul 2019"
"BoxOffice":"$858,373,000"
"Production":"Marvel Studios, Walt Disney Pictures"
"Website":"N/A"
"Response":"True"
}

我得到的回应:

Response {type: "cors", url: "https://movie-database-imdb-alternative.p.rapidapi.com/?i=tt4154796&r=json", redirected: false, status: 200, ok: true, …}
body: (...)
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "cors"
url: "https://movie-database-imdb-alternative.p.rapidapi.com/?i=tt4154796&r=json"
__proto__: Response

有人知道为什么我的代码没有显示正确的值吗?

奖励:当我执行 console.log(response.url);然后单击 chrome 开发人员工具中的 URL,我得到了正确的值。但这仅适用于开发人员工具,并且 URL 与我调用的 URL 相同。我觉得 API 有问题。

【问题讨论】:

    标签: javascript api fetch response imdb


    【解决方案1】:

    简单易学

    使用 res.json() 从 api 获取 json 数据。

    
    fetch("https://movie-database-imdb-alternative.p.rapidapi.com/?i=tt4154796&r=json", {
        "method": "GET",
        "headers": {
            "x-rapidapi-key": "my x-rapidapi-key (don't want to show)",
            "x-rapidapi-host": "movie-database-imdb-alternative.p.rapidapi.com"
        }
    })
        .then(response => {
            console.log(response.url);
            return response.json()
        })
        .then(data => {
           console.log(data)
        })
        .catch(err => {
            console.error(err);
        });
    
    
    

    【讨论】:

      猜你喜欢
      • 2015-02-10
      • 2016-06-19
      • 1970-01-01
      • 2013-04-12
      • 2017-01-01
      • 2019-05-21
      • 2015-08-13
      • 2012-11-22
      • 2015-05-25
      相关资源
      最近更新 更多