Server Requests Flashcards
What does AJAX stand for?
Asynchronous Javascript and XML
Why do we use AJAX requests?
AJAX requests allow us to make requests to the server after the initial page loads.
What is the XHR API?
XMLHttpRequest API allows us to make many types of requests to the server and accept different forms of data.
What does JSON stand for?
JavaScript Object Notation
How do we start the process of creating an XMLHttpRequest?
We define a variable and set it equal to a new XMLHttpRequest object
const xhr = new XMLHttpRequest( )
Generally, how do we store the url that will be used for an XHR API call?
We set the url string equal to a variable
const url = “http://www.api-to-call.com/endpoint”
How do we indicate the response type we expect from an XHR API request?
xhr.responseType = ‘json’
How do we indicate what needs to be done when an XHR API call is finished?
We write a function that monitors the readystate of the XHR API call and include a conditional statement within that function to run code on a state of DONE.
What does the readystate function look like for an XHR API call?
xhr.onreadystatechange = ( ) => {
if(xhr.readyState === XMLHttpRequest.DONE){
return xhr.response (or other code)
}
{
How do we access the response of an XHR API call?
We can return the xhr.response from the XHR API call within the xhr.onreadystatechange function.
After we have written our xhr code to send a XHR API call, how do we initiate the call?
xhr. open(‘GET’, url) = a GET request to the const url
xhr. send( ) = to send the request
When do we use JSON.stringify?
We use JSON.stringify to convert data to a JSON string so that it can be sent to a server or database.
How do we typically convert data to a JSON format to send to a server or database?
We can use the JSON.stringify( ) to turn our data into a JSON string.
const dataToSend = JSON.stringify({data: ‘value’});
When making an xhr ‘POST’ request, what is the primary difference in code from a ‘GET’ request?
There are three primary differences:
- We include data that must be converted to a type accepted by the server or database - JSON is often the type, so a JSON.stringify is needed
- The xhr.open( ) command contains ‘POST’ instead of ‘Get’ as the first parameter.
- The xhr.send( ) command now includes our converted data as a parameter, ‘GET’ requests do not include a parameter in the send method.
Can POST requests also request information?
Yes, a POST request can both introduce and request information from another source