Quiz
- when we use ajax, does the browser still send a http request to flask like with a normal form submission? if no, why? if yes, why?
Well, there certainly is a request that gets sent to the server, and it comes from the browser, so one answer is *yes*.
A more nuanced answer is that there are two ways that the browser can send a request:
- The normal, old-fashioned way: the browser submits the request, waits for the response, and *replaces* the current window/tab with the response. This is what we've been doing all semester, and although it's "old," there's nothing wrong with it. Or, at least, not much wrong with it.
- The Ajax way: Some JS code running in the browser sends an
asynchronous request and designates a response
handler. When the response arrives, the handler gets it
and does whatever it does. It might:
- replace the whole page (least likely),
- it might do nothing visible (very possible), especially if the point of the request was some state change like unsubscribing from a mailing list or favoriting an item.
- or it might make surgical updates to the page, such as updating a "like" count, adding a heart to a "liked" item, or inserting a confirmation that they are now un-subscribed from the mailing list.
So, what are the disadvantages of the "old" approach? If the replacement page is 99% the same as the current page, that's a lot of network traffic, screen layout, loss of context and delay for no good advantage.
Disadvantages of Ajax are that it requires the client to have JS (in the past, many assistive technologies, such as browser for the blind, did not support JS, but most now do). Another is web crawlers and scrapers and such are harder to write; you can't just look for hyperlinks and forms.
Thanks for this question!
- No questions, thanks!
I'm glad!