Captain AJAX
Tags: javascript, beginners, firstyearincode
Published: 2019-10-07 12:42:39

Have you ever wondered how asynchronous requests are made on web pages?

Have you ever wondered what "asynchronous requests" are?

As an average internet-consuming human...probably not, but if you're here, you're probably no longer just that, but now an(at least semi) internet-comprehending human, and that's pretty cool!

One of the most important characteristics of modern web browsing is the use of Asynchronous JavaScript and XML requests, or 'AJAX'. Unbeknownst to the common web user, these 'requests' are happening in the background, pretty much <i>all the time</i>.


![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ef3s058f5zj4j1tvnkp4.gif)

AJAX radically changed the face of the internet by keeping it static, yet dynamic. As a child of the 90's, I remember when internet access was still fairly limited and running on dial-up. Back then, web pages were rendered 'synchronously', meaning if you clicked on a hyperlink, the page would have to completely reload, because at that time, data requests were sent from the client(a user's computer) to the server, and then responded to one at a time. (I'm sure by now you realize that half the time spent surfing the web in the 90's was just waiting for the page to load.)

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/yukotr37lzbnjog9paan.gif)


Then, in the 1996, something really magical happened; Microsoft Internet Explorer introduced the <iframe> tag and changed the world, and my life.


![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/sbg2btnoel7p0c6mc5w4.gif)


Requests could now be sent to the server in the background of a web page without the face of the web page changing, making websites far more user friendly and speedy. Because of this async wave, new forms of communication and interaction with the internet were made possible. 


![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/pz5ie69xso6hzdafytc8.png)


You may not have ever realized it, but any time you login to any website, use autocomplete, or visit a chatroom, AJAX is happening in the background. Let's break down what's happening in this chart step by step.


```
Asynchronous processing model

1. An event occurs in a web page, aka the client (like a click event)
2. An XMLHttpRequest(XMR) object is created by JavaScript
3. The XMR object sends a request to a web server using JSON or XML
4. The server processes the request, usually called a 'GET' request 
5. The server sends a response back to the web page
6. The response is read by JavaScript
7. Proper action is performed by JavaScript, HTML/CSS, 
and the DOM to render the information to the screen, uninterrupted

```

What this is really saying is that AJAX is not a technology in itself, but a compilation of technologies working together to produce a result. 

In it's earliest forms, AJAX requests were incredibly convoluted and easy for developers to get caught up in their syntax. Luckily, modern libraries and frameworks have abstracted away a lot of this syntax but the underlying technology still remains the same, to help you understand what I mean by this, let's use the 90s classic cartoon 'Captain Planet' to get more insight.
 

If you've never heard of CP here's a little background: He was awesome! However, his power could only be summoned by the combined powers of his earthly friends "The Planeteers".


![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/9zjjid6c4t5hbyk34a34.gif) 
<strong> DOM (Document Object Model) - <i>Earth</i> </strong>
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/13vwrt4gxy27u1ast5nv.png)
The very structure on which every web page on the internet is built.


<b>HTML/CSS - <i>Fire</i></b>
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/70rmy58hwx3lf00n60sn.gif)   
The DOM is represented by the HTML backing structure and Cascading Style Sheets for it to be fleshed out. 
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/6yhq0axy0hzg29kyosfy.png)
HTML is how elements on the page are rendered and CSS is what they look like.



<b>JSON (JavaScript Object Notation)
 && XML/XMR (Extensible Markup Language) - <i>Wind</i></b>
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/rlwcce9bnv4u1g1prmt7.gif)

Original AJAX requests were sent via XML, but this syntax was very easy for programmers to get tangled up in, and the move was made to JavaScript Object Notation or JSON to provide additional clarity..so yes, technically, AJAX is really 'AJAJ'. 
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/nmj2hp4agbxc85gfx23d.png)
This would be considered the wind element due to the parsing and transference of data back and forth across the web.
   

<strong>JavaScript- <i>Water</i></strong>
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/b4ouc2oycbrbl7ixdung.gif)
I would consider JavaScript the water element, due to it's important role in the control flow of data being requested from the server. 
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/35aa6j2yjk2u3f1spba4.png)
Once the server has responded to the client request, again, it's JavaScript's job to implement a callback function that tells this freshly received data what to do.



<b>XMLHttpRequest Object - Heart</b>
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/f1e1d7fb7f3ade2z091i.gif)
The heart of the entire operation is the XMR object, which is available in the JavaScript environment. This very special object's job is to communicate back and forth with the server to produce asynchronous requests.
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/cbxr1x44vknio3g3ln17.png)



By the power of all these technologies combined,
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/te3rvea1ut2cgn8roc8f.png)


you TOO can 'GET' an AJAX request.
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/gp80nquh94o0px9uc8jx.gif)




![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/rdi8f39rsdj687fc739y.gif)


(As long as it's not in classic syntax because Google HATES that.)