Soy nuevo en la codificación y estoy atascado. Estoy tratando de tomar la respuesta JSON de la API REST de GitHub. Estoy usando un archivo de script externo pero no funciona. Cuando traté de usarlo en HTML con la etiqueta del script, estaba dando un error. Probé una forma que se usa para autenticar el token en fetch() y da un error al respecto. Se llama "encabezado", no lo sé. Seré feliz si ustedes me ayudan.
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>Guion:
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()); }Reemplace todo en el archivo js con esto:
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); });No hay necesidad de async ya que .then() se encarga de eso
Probé tu código y parece que los encabezados son innecesarios
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()); }