After I pulled data from a mock API using fetch, I managed to loop the data and have it posted on my console. I'm having trouble posting the data on a div to be shown on the page. Help would be appreciated. Thanks in advance!
$(document).ready(function() {
const fetchData = async() => {
const response = await fetch('https://n161.tech/api/dummyapi/user?limit=5&page=1');
const data = await response.json();
// items = JSON.stringify(data);
console.log(data);
for (var i = 0; i < data.data.length; i++) {
var obj = data.data[i];
console.log(obj);
}
}
fetchData();
});
html for reference(not designed with CSS yet
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Pictures&users</title>
<link rel="stylesheet" href="main.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<!-- <script scr="fetch.js"></script> -->
<script src="fetch.js"></script>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col div1">Column</div>
<div class="col">Column</div>
</div>
</div>
</body>
</html>
Basically I want one of the DIVs to post certain values from the fetched JSON. The other would be used for a hardcoded image. I I'll of course add the
and I need for the specific values. I'm having trouble achieving this in vanilla JS.
the JSON used for reference
{
"data": [
{
"id": 6,
"nameTitle": "ms",
"firstName": "Madeleine",
"lastName": "Brown",
"image": "https://randomuser.me/api/portraits/women/65.jpg"
},
{
"id": 7,
"nameTitle": "mr",
"firstName": "Akseli",
"lastName": "Ruona",
"image": "https://randomuser.me/api/portraits/men/90.jpg"
},
{
"id": 8,
"nameTitle": "ms",
"firstName": "Jennifer",
"lastName": "West",
"image": "https://randomuser.me/api/portraits/women/53.jpg"
},
{
"id": 9,
"nameTitle": "mrs",
"firstName": "Carlucia",
"lastName": "Das neves",
"image": "https://randomuser.me/api/portraits/women/52.jpg"
},
{
"id": 10,
"nameTitle": "mrs",
"firstName": "Silviane",
"lastName": "Peixoto",
"image": "https://randomuser.me/api/portraits/women/76.jpg"
}
],
"total": 5,
"page": 1,
"limit": 5
}
The basic idea is to build the html as string then append it to html so you need to add id
to your main div
<body>
<div class="container-fluid">
<div id="row" class="row">
<div id="test" class="col div1">Column</div>
<div class="col">Column</div>
</div>
</div>
</body>
then update your js
$(document).ready(function() {
const fetchData = async() => {
const response = await fetch('https://n161.tech/api/dummyapi/user?limit=5&page=1');
const data = await response.json();
// items = JSON.stringify(data);
console.log(data);
div = document.getElementById('row');
for (var i = 0; i < data.data.length; i++) {
var str ='<div>'+data.data[i].nameTitle +' '+data.data[i].firstName +' '+ data.data[i].lastName+ '</div>';
str +='<img src="'+data.data[i].image+'" alt="'+data.data[i].firstName + data.data[i].lastName+'">';
div.insertAdjacentHTML( 'beforeend', str );
}
}
fetchData();
You can easily modify the output as you want and also use bootstrap class
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.