the interesting bit in this code is the bit that goes
fetch("http://127.0.0.1:5000/api/open-kitty-location")
since this sends an http request to a local server… my flask app
another interesting part is
.then(r => r.json())
which takes the http response and parses it as JSON
Parsing JSON means converting a JSON-formatted string into a data structure, such as a dictionary or list, that can be easily manipulated in a programming language.
In simple terms, .then() in JavaScript is used to run a function after an asynchronous task, like fetching data, is successfully completed.
In layperson’s terms, .then(r => r.json()) takes the response from a web request and converts it into a JavaScript object that you can easily work with.
The .then(r => r.json()) method converts the response into a JavaScript object, typically an object or array, depending on the JSON structure returned by the web request.
appreciate that the second .then uses the result from the first .then
.then(data => alert(data.msg)) means that after successfully getting some data, it will show a pop-up alert displaying the message stored in the msg property of that data.
In this context, “data” is not a specific term but rather a placeholder name for the variable that holds the information returned from a web request. You can name it anything you like, as it simply represents the content being processed in the function.
<button id="kitty-location">open kitty</button>
<script>
document.getElementById("kitty-location").addEventListener("click", () => {
fetch("http://127.0.0.1:5000/api/open-kitty-location?location=")
.then(r => r.json())
});
</script>