-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
25 lines (21 loc) · 906 Bytes
/
Copy pathscript.js
File metadata and controls
25 lines (21 loc) · 906 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
document.addEventListener('DOMContentLoaded', function() {
const fetchButton = document.getElementById('fetch-data');
const apiResult = document.getElementById('api-result');
const apiData = document.getElementById('api-data');
fetchButton.addEventListener('click', async function() {
try {
fetchButton.textContent = 'Loading...';
fetchButton.disabled = true;
const response = await fetch('/api/data');
const data = await response.json();
apiData.textContent = JSON.stringify(data, null, 2);
apiResult.style.display = 'block';
} catch (error) {
apiData.textContent = `Error: ${error.message}`;
apiResult.style.display = 'block';
} finally {
fetchButton.textContent = 'Fetch Data from API';
fetchButton.disabled = false;
}
});
});