Dealing with XHR requests
Asynchronous JavaScript, commonly known as Ajax, has become an important part of web applications over the last decade or so. The built-in XMLHttpRequest (XHR) object in a browser is used to execute Ajax on web pages. With the advent of single-page applications and JavaScript application frameworks such as Angular, Vue, and React, this technique of web development has risen exponentially. In this recipe, we will implement an Ajax request to facilitate asynchronous communication between the backend and the frontend.
Note
In this book, I am opting to use Ajax to demonstrate async
requests because it is simpler to understand and demonstrate and keeps the focus of the book on Flask. You can choose to use any JavaScript platform/framework. The Flask code would remain the same, while the JavaScript code would have to change according to the framework that you used.
Getting ready
Flask provides an easy way to handle the XHR requests in the view handlers...