AJAX stands for Asynchronous JavaScript and XML.
It is often used as a term to say:
- Get (or send) some data to the server
- Wait for the server to send something back
- Update the page without refreshing it
XML
XML stands for Extensible markup language. It was a standard we used to pass information between browsers and servers in the past.
But we rarely use XML now. We use another format called JSON. Youโll learn about JSON in the next lesson.
XMLHttpRequest
XMLHttpRequest (XHR for short) is a method we used to send and retrieve data. Itโs the technology that made Ajax possible.
If you want to get data back from a server, you need to send something to the server first.
We call this data you send to the server a request. We call the stuff the server sends back a response. The action (sending a request and getting a response) is called a fetch.
To send a request, we need to create an XMLHttpRequest object first.
const request = new XMLHttpRequest()We want to know when a response comes back from the server. To do so, we to listen for the load event.
request.addEventListener('load', e => { // do something})Next, we want to construct the request. This is how it looks like:
request.open(method, link)method is the type of request you want to send. We will explain what method is in a later lesson. For now, letโs set method to get.
link is the place you want to request information from. The requested information is also called a resource. If we want to get a list of my Github repositories, we can use https://api.github.com/users/zellwk/repos.
request.open('get', 'https://api.github.com/users/zellwk/repos')Finally, we need to send the request out.
request.send()Hereโs the entire XMLHttpRequest we created together.
const request = new XMLHttpRequest()request.addEventListener('load', e => { /* do something */})request.open('get', 'https://api.github.com/users/zellwk/repos')request.send()The server must accept your request
Servers can choose to accept or reject your request. If the server does not accept your request, youโll get an error that says โNo โAccess-Control-Allow-Originโ header is present on the requested resourceโ.
request.open('get', 'https://google.com')
https://google.com The callback
Weโre interested in the event target (the XMLHttpRequest itself). If you log the target, youโll see the response.
request.addEventListener('load', e => { console.log(e.target)})
The response property contains the data you requested from the server. This data is often called the body or the payload.
If you pay attention to response, you will notice that the response looks like a string. This string is a JSON string (which brings us to the next lesson on JSON).
A note on rate limits
You can send 60 requests to Github every hour. If you send more, youโll get an 403 (forbidden) error that says.
This 60 request per hour thing is a rate limit. API creators use them to limit the number of requests you can make. They help prevent the API from crashing due to too many requests.
The limit can increase when you are authenticated (or logged in). For example, Github lets you send up to 5000 authenticated requests every hour.
The only way to โfixโ an error due to rate limits is to wait until your limit refreshes. (In short, try again later).
Exercise
Answer these questions:
- What is AJAX?
- Why do we use Ajax?
- What is a request?
- What is a response?
- What is a fetch?
- What is a rate limit?
Do this:
- Send a request to Github. Ask for a list of your repositories.
- Hint: replace
zellwkwith your Github username. - If you donโt know your Github username, request for a list of my repositories.
- Hint: replace
- Log the response from the server.
Welcome! Unfortunately, you donโt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson