Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am currently trying to fetch a URL an display an array of images on a webpage. I have my HTML and JS code below,

I am currently trying to fetch a URL an display an array of images on a webpage. I have my HTML and JS code below, my problem is when I try to set the image source as the URL (image.src = data[i];) I get the error: GET file:///C:/Users/Rachel/Downloads/Module%209%20Competencies/21/undefined net::ERR_FILE_NOT_FOUND.

Both of the files were extracted from a zip folder and are in the same directory. Any help or resources are much appreciated to fix this issue, thanks so much!

HTML:

Competency 21

Javascript:

function load() {

fetch("https://dog.ceo/api/breed/husky/images") .then(function(result){ return result.json(); }) .then(function(data){ createHTML(data); }); }

function createHTML(data){

let main = document.getElementsByTagName("main")[0]; let h1 = document.createElement("h1"); let ul = document.createElement("ul"); h1.innerHTML = "Huskies"; main.appendChild(h1); let count = data.length; for (let i = 0; i < count; i++) {

let li = document.createElement("li"); let image = document.createElement("img"); let label = document.createElement("label");

label.innerHTML = data[i]; image.src = data[i]; // **When this is removed, there are no errors but the images do not display. When this is added, I get the ERR_FILE_NOT_FOUND error. image.width = 100; image.height = 100;

li.appendChild(image); li.appendChild(label); ul.appendChild(li); }

main.appendChild(ul); }

document.addEventListener("DOMContentLoaded", load);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 17th International Conference Dasfaa 2012 Busan South Korea April 2012 Proceedings Part 1 Lncs 7238

Authors: Sang-goo Lee ,Zhiyong Peng ,Xiaofang Zhou ,Yang-Sae Moon ,Rainer Unland ,Jaesoo Yoo

2012 Edition

364229037X, 978-3642290374

More Books

Students also viewed these Databases questions

Question

Challenges Facing Todays Organizations?

Answered: 1 week ago