Creating a Custom Ajax Wrapper

The Ajax wrapper that we will be creating is simply called Ajax, and is an object that follows the Singleton pattern, which we will cover in detail in Chapter 14, “Singleton Pattern.” This object will handle all requests to the server through the XHR as a single object that will always be reliable and never change states—hence the Singleton pattern. This way, any object that needs to make a request will do so through the same object, keeping the requests structured and organized. In order to create this object, we first need to simply construct it as in Listing 6.1.

Listing 6.1. Constructing the Ajax Object (Ajax.js)
Ajax = {};

After the object is constructed, we can create all the methods we will need to call ...

Get Ajax for Web Application Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.