我是編碼新手,我被卡住了。我正在嘗試從 GitHub 的 REST API 獲取 JSON 回應。我正在使用外部腳本檔案,但它不起作用。當我嘗試在帶有腳本標記的 HTML 中使用它時,它給出了一個錯誤。我嘗試了一種用于在 fetch() 中驗證令牌的方法,它給出了一個錯誤。它被稱為“標題”,我不知道。如果你們幫助我,我會很高興。
HTML:
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>GitHub API Trial</title>
</head>
<body>
<script src="github.js"></script>
</body>
</html>
腳本:
getData();
const api_url = 'https://api.github.com/users/alisirri/repos';
async function getData() {
const response = await fetch(api_repo_url,
{
headers: {
authorization: "TOKEN"
}
}
)
console.log(await response.json());
}
uj5u.com熱心網友回復:
我測驗了你的代碼,看起來標題是不必要的
getData();
async function getData() {
const api_url = "https://api.github.com/users/alisirri/repos";
const response = await fetch(api_url);
console.log(await response.json());
}
uj5u.com熱心網友回復:
將 js 檔案中的所有內容替換為:
const api_repo_url = "https://api.github.com/users/alisirri/repos";
fetch(api_repo_url, {
headers: {
authorization: "TOKEN",
},
})
.then((response) => response.json())
.then((data) => {
console.log(data);
});
也不需要異步,因為 .then() 會解決這個問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/357451.html
標籤:javascript html
