Advertisements
- Create an HTML file and add a container where you want to display the data from the API.
- In the same HTML file, create a JavaScript function that uses AJAX to send a request to the API endpoint and receive the response.
- Parse the response using JSON.parse() method and update the HTML container with the received data.
Here's an example code snippet:
HTML:
<div id="data-container"></div>
JavaScript:
function getData() {
var xmlhttp = new XMLHttpRequest();
var url = "https://your-api-endpoint.com/data";
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var data = JSON.parse(this.responseText);
document.getElementById("data-container").innerHTML = data;
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
In this example, the getData() function sends a GET request to the API endpoint at "https://your-api-endpoint.com/data". Once the response is received, the function parses the JSON data and updates the HTML container with the new data.
You can call this function on an event like a button click or as soon as the page loads.
Advertisements
0 Comments