As you have seen earlier in this chapter, we used a responder class that produced HTML. For the purposes of answering AJAX requests, we develop a responder class that produces JSON. For this purpose, we create a file, /path/to/repo/chapters/06/src/web/responder/json.py, in which we define a JsonResponder class.Â
As we plan to return JSON, in the __init__() method, we can add a Content-Type header set to application/json:
# web.responder.json
import json
class JsonResponder :
headers = []
data = []
def __init__(self) :
self.addHeader('Content-Type: application/json')
As with the Html responder class, we define a method that allows us to add additional headers. Instead of the addInsert() method used in the Html responder class, we define a method, addData(), which simply adds to an internal dictionary:
def addHeader(self, header) :
self.headers.extend([header])
def addData...