-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
36 lines (28 loc) · 1004 Bytes
/
script.js
File metadata and controls
36 lines (28 loc) · 1004 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
26
27
28
29
30
31
32
33
34
35
document.getElementById("exchange_form").addEventListener('submit', async function(event) {
event.preventDefault();
let amount = document.getElementById("amount").value;
let current_currency = document.getElementById("current_currency").value;
let destinated_currency = document.getElementById("destinated_currency").value;
const endpoint = "http://127.0.0.1:8000/exchange_currency";
try {
const response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
"amount": Number(amount),
"from_currency": current_currency,
"to_currency": destinated_currency
})
});
if (!response.ok) {
throw new Error(`error exchanging: ${response.status} ${response.statusText}`);
}
let data = await response.json();
const result = document.getElementById("result_p")
result.innerHTML = `${data.total} ${destinated_currency}`
} catch (error) {
console.error("problem with exchange", error);
}
});