| 1 |
{% extends 'base.html' %} |
| 2 |
|
| 3 |
{% block intro %} |
| 4 |
<a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch"><code>fetch</code></a> |
| 5 |
is the <em>modern</em> plain JavaScript way to make requests. It's |
| 6 |
supported in all modern browsers. |
| 7 |
{% endblock %} |
| 8 |
|
| 9 |
{% block script %} |
| 10 |
<script> |
| 11 |
function addSubmit(ev) { |
| 12 |
ev.preventDefault(); |
| 13 |
fetch({{ url_for('add')|tojson }}, { |
| 14 |
method: 'POST', |
| 15 |
body: new FormData(this) |
| 16 |
}) |
| 17 |
.then(parseJSON) |
| 18 |
.then(addShow); |
| 19 |
} |
| 20 |
|
| 21 |
function parseJSON(response) { |
| 22 |
return response.json(); |
| 23 |
} |
| 24 |
|
| 25 |
function addShow(data) { |
| 26 |
var span = document.getElementById('result'); |
| 27 |
span.innerText = data.result; |
| 28 |
} |
| 29 |
|
| 30 |
var form = document.getElementById('calc'); |
| 31 |
form.addEventListener('submit', addSubmit); |
| 32 |
</script> |
| 33 |
{% endblock %} |