API 并获取 IMDB 替代电影数据库
API and fetching IMDB alternative movie database
我正在使用这个 API - https://rapidapi.com/rapidapi/api/movie-database-imdb-alternative
我正在使用 JavaScript 实现,但看不到我应该看到的值。这不是我第一次使用 APIs,但我不理解这种行为。
我的代码:
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":"8,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 与我调用的相同。我觉得 API.
有问题
轻松愉快
使用 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);
});
我正在使用这个 API - https://rapidapi.com/rapidapi/api/movie-database-imdb-alternative 我正在使用 JavaScript 实现,但看不到我应该看到的值。这不是我第一次使用 APIs,但我不理解这种行为。
我的代码:
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":"8,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 与我调用的相同。我觉得 API.
有问题轻松愉快
使用 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);
});