Book Image

Flask Framework Cookbook

By : Shalabh Aggarwal
Book Image

Flask Framework Cookbook

By: Shalabh Aggarwal

Overview of this book

Table of Contents (19 chapters)
Flask Framework Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Dealing with XHR requests


Asynchronous JavaScript XMLHttpRequest (XHR), commonly known as Ajax, has become an important part of web applications over the last few years. With the advent of one-page applications and JavaScript application frameworks such as AngularJS, BackboneJS, and more, this technique of web development has risen exponentially.

Getting ready

Flask provides an easy way to handle the XHR requests in the view handlers. We can even have common methods for normal web requests and XHRs. We can just look for a flag on our request object to determine the type of call and act accordingly.

We will update the catalog application from the previous recipe to have a feature that will demonstrate XHR requests.

How to do it…

The Flask request object has a flag called is_xhr, which tells us whether the request made is an XHR request or a simple web request. Usually, when we have an XHR request, the caller expects the result to be in the JSON format, which can then be used to render content...