Understanding Ajax Database Interaction
We have already begun to cover Ajax database interaction in previous chapters, but this chapter will takes us to a deeper understanding of the interaction model by explaining every step. Database interaction with Ajax allows developers to create interaction paradigms that can exist only with this set of technologies. The actions that occur to create database interactions with Ajax are the same as a standard interaction, but the way in which the request process occurs is quite different. The actions that exist in both interactions start with a user interaction on the front end, which fires an HTTP request to the server where a server-side language makes a query on the database. What happens in the databasefor example, an insert, a delete, or a selectwill all occur exactly the same as well. Again, the only difference in these two processes is the way in which they make the request. Creating connections to a database without interrupting the user experience with browser refreshes is definitely unique to Ajax and is what makes the XHR different from a standard HTTP request.
It is true that Ajax can easily be misused and often is, but it can be extremely useful in the right situations, such as when the sample application receives and/or sends emails. Imagine if you were in the middle of reading an email and your browser refreshed in order to get new messages. This would be an unusable situation because you would completely lose your place and have to reorient yourself with the interface that may have changed due to new email messages. Therefore, this is one of those situations where it would be perfect to use Ajax to make database connections. We can make requests to the server with the Data Reflection pattern we covered earlier in Chapter 17, "Data Reflection Pattern," receive updates if new messages exist, and replenish the inbox with the new data from the database. This solution also enables us to keep the other areas of our application as is or untouched while we update the necessary data inconspicuously. In other words, using the components we have built, we can replenish the data in the data grid and tree components with new messages while leaving the accordion component untouched as the user continues to read an open email message. In this chapter, we will dive into the server-side code and learn each of the steps that make a database-enabled XHR possible.