0
I need some help with javascript. I’m trying to show in my index.html the data I’m consuming from a json. But when opening the index and waiting for the document to be loaded the items comes with Undefined.
$(document).ready(function () {
var products = "";
var url = "https://api.myjson.com/bins/10p600";
$.ajax({
url: url,
dataType: "json",
success: function (response) {
if(response[0].erro) {
$("h2").html(response[0].erro);
}else {
for(var i = 0; i < response.length; i++) {
products += "<div>";
products += "<div>" + response[i].imageName + "</div>";
products += "<h4>" + response[i].name + "</h4>";
products += "<p>" + response[i].price + "</p>";
products += "<p>" + response[i].paymentConditions + "</p>";
products += "</div>";
}
$("#myproducts").html(products);
}
}
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Teste Front-End</title>
</head>
<body>
<div id="myproducts"></div>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="teste.js"></script>
</body>
</html>
When so remember to post the source JSON, questions should not depend on external links. Links to better understand how Sopt works: [Tour], [Ask], Manual on how NOT to ask questions and [Help].
– Bacco