'How could I show JSON from url in HTML using Javascript
good afternoon. I'm working on a project where I get a JSON and I have to display it in HTML and I'm not able to do that. In the console I can display all the information correctly, but I cannot display it in the HTML. What do I have to do? Here's the code and thanks for all the help. The error in console is:
Uncaught SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at Object.success (index.html:21:26)
at i (jquery-3.2.1.min.js:2:28017)
at Object.fireWith [as resolveWith] (jquery-3.2.1.min.js:2:28783)
at A (jquery-3.2.1.min.js:4:14035)
at XMLHttpRequest.<anonymous> (jquery-3.2.1.min.js:4:16323)
And HTML code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link id="mystylesheet" type="text/css" rel="stylesheet" href="style.css">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>
<body>
<div id="wrapper">
<h1 class="city"></h1>
<h2><span id="tags">0</span>ºC</h2>
</div>
<script>
const urlJsonString = $.getJSON('https://api.openweathermap.org/data/2.5/onecall?lat=38.7267&lon=-9.1403&exclude=current,hourly,minutely,alerts&units=metric&appid=ecef7e88947b6303121bb900373e41d2', function (data) {
let urlJson = JSON.parse(urlJsonString)
let [dt1, dt2, dt3, dt4, dt5, dt6, dt7, dt8] = urlJson.daily.map(({ dt, temp: { day }, weather: [{ description, icon }] }) => ({ dt, day, description, icon }))
let result = `Data: ${urlJson}`
$(".city").html(result);
});
</script>
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
