How can I use Ajax to make JSON data class?

Asked 1 years ago, Updated 1 years ago, 330 views

How do I get JSON data via asynchronous communication in a class format?
I would like to be able to display the image after pressing the button button on HTML, but I looked into various class methods, but I didn't understand.If you understand, please help me.
There are many things I don't understand just a few months after I started studying as an engineer, so please be gentle with me.

const btn= document.getElementById("btn");
const dogImg = document.getElementById("dogImg");

// Click the button to activate the event
btn.addEventListener("click",()=>{
  fetch("https://example.com/api")// API URL
    .then(response)=>{
      return response.json();
    })
    .then((myJson)=>{
      // Put the URL image you got in the img tag
      dogImg.src=myJson ["message"];
    });
});

javascript json ajax

2023-01-18 23:06

1 Answers

For example, a request such as "Replace integer multiplication with addition" or "Replace trinomial operator with if" can be written with that much information.However, the code in the questionnaire doesn't know what should be classified, and I can't think of any particular advantage in using the class, so the answer is "no need to rewrite the code using the class."

There was a candidate who wondered if the class could be involved in the following situations.

  • I want to pass an instance of a class to the second argument of addEventListener()
  • I want to pass a class of methods to the second argument of addEventListener()
  • I want to define custom element that incorporates the behavior of this click


2023-01-19 00:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.