[英]Unhandled Rejection (SyntaxError): Unexpected end of JSON input with Get request fetch
[英]× Unhandled Rejection (SyntaxError): Unexpected end of JSON input
我有一個 if 語句,用於檢查是否定義了訪問令牌,如果是,則運行這 2 個獲取請求。 我有第一個 fetch 請求返回我想要的信息。 我只是想記錄一個單獨的 fetch 請求的輸出,該請求從一個單獨的端點獲取信息......我的本地服務器不斷返回上面提到的錯誤,我無法弄清楚為什么
這是我試圖運行的代碼。
componentDidMount() {
let accessToken = queryString.parse(window.location.search).access_token;
if (accessToken != undefined) {
//working fetch request..
fetch('https://api.spotify.com/v1/me', {
headers: {'Authorization': 'Bearer ' + accessToken}
}).then((response) => response.json())
.then((data) => {
this.setState({
serverData: {
user: {
name: data.display_name,
profileLink: data.external_urls.spotify,
images: data.images
}
}
});
console.log(data);
console.log(this.state.serverData);
})
// breaks on this line
fetch('https://api.spotify.com/v1/me/player/currently-playing', {
headers: {'Authorization': 'Bearer ' + accessToken}
}).then((response) => response.json())
// here i am simply trying to log the returned data
.then((data) => console.log(data))
}
}
現在我相信這可能是一個小的語法錯誤,但我似乎無法解決它,非常感謝任何煽動這個錯誤的人,謝謝!
加入.catch(error => console.log(error))
塊的后.then()
塊,看到在控制台上的錯誤,讓我知道是什么錯誤。 你會得到更多這樣的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.