July 2019
Beginner to intermediate
302 pages
9h 38m
English
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 JSON format, which can then be used to render content in the correct place on the web page without reloading the page.
So, let's say we have an Ajax call to fetch the number of products in the database on the home page itself. One way to fetch the products is to send the count of products along with the render_template() context. Another way is to send this information over as the response to an Ajax call. We will implement the latter to see how Flask handles XHR:
from flask import request, render_template, jsonify ...