The primary purpose of getState() is to retrieve the LED's current state from the server. It uses the JQuery get() method to make an HTTP GET request to our API server's /led resource. We saw, in the previous section, that the URL path, /led, is mapped to the LEDControl Python class, and because we're making a GET request, it's LEDControl.get() that will receive and handle our request:
// GET request to server to retrieve LED state.
function getState() {
$.get("/led", function(serverResponse, status) { // (2)
console.log(serverResponse)
updateControls(serverResponse) // (3)
});
}
The server's response is contained in the serverResponse parameter on line (2), which is passed to the updateControls() function on line (3) to update the web page controls. We'll cover this method shortly.
While getState...