Quiz

  1. How does Ajax handle error management and fallback strategies for when requests fail or when a user has disabled JavaScript in their browser?

    Great question. Ajax relies on JS, so if the user has disabled JS, we are out of luck. That's why it's best to have progressive enhancement: build a "classic", JS-free website, and then use JS to hide those old things and replace them. If JS is disabled, you still have a working site. The reading talked about this.

    However, a recent survey showed that 90+ percent of blind people used browsers with JS enabled. So, this is less of a concern than it used to be.

    If a request fails, hopefully the browser gets an error response or times out.

  2. For jQuery methods with the datatype parameter, if we specify a type and the response doesn't match the specified type, what happens?

    Presumably an error of some type. You can set up an ajax error handler. You can also chain a .fail() handler onto a particular request. See jquery.post

  3. For the Ajax way, how is it updating the like count in the database? In the graph it falls under app.post handler, but I didn't see any code for updating.

    I think you overlooked this code:

    
    // increments the "likes" for a movie and returns the updated document
    
    async function likeMovie(tt) {
        const db = await Connection.open(mongoUri, DBNAME);
        const result = await db.collection(MOVIES)
              .updateOne({tt: tt},
                         {$inc: {likes: 1}},
                         {upsert: false});
        doc = await db.collection(MOVIES).findOne({tt: tt});
        return doc;
    }
    
    

    That function is called from both the classic and the Ajax handlers.

  4. I am little confused about
    app.post('/likeAjax/:tt'
    calling
    likeMovie(tt)
    and
    likeMovie(tt)
    posting to likeAjax/tt Are they using each other at the same time? How does that work?

    My bad. I had a front-end JS function to launch an Ajax request to "like" a movie, and a back-end function to increase the like count in the database, and I called them both likeMovie. I'll have to come up with new names that make this clearer. Maybe likeMovieViaAjax and likeMovieInDb.

  5. We can't flash messages in Ajax because rendering does not occur. Is it possible to use 'res.send' to get messages to the browser?

    The Ajax request gets to respond to the browser. That's where to put the error messages.

  6. I think going over the concept of Ajax in class will help marinate the concepts into my smooth brain.

    You have a big, wrinkley brain. You'll get this.