Communicating with servers in SPAs:AJAX, XHR, CORS, and Web sockets (Week 8) Flashcards
What is AJAX?
✨✨✨
Asynchronous Javascript and XML.
A set of techniques for accessing web servers from a web page.
Makes it possible to update parts of the web page without reloading the page (update data behind the scene asynchronously).
How do you get data from the server without refreshing the page?
AJAX and XHR.
Fires requests to get data and executes http methods programatically using javascript
What are the problems of events?(6)
❌ reactive (waiting before act)
❌ asynchronous (don’t know when)
❌ dependencies (between assets on page)
❌ system & user (system can initiate event AJAX)
❌ volume (# of events handled)
❌ changeability (SPA events change)
What are some solutions to problems with events?(6)
✔️ handlers
✔️ callbacks
✔️ promises
✔️ reactive programming
✔️ async functions
✔️ generators
Why has REST replaced SOAP?
✨✨✨
✔️ Easier to use
✔️ Accepted through firewalls
SOAP has a very heavy infrastructure
✔️ REST works well with AJAX
What is the difference between AJAX and $.ajax()
AJAX is a library
$.ajax() is a call to the library
20
$(document).ready(function(){
21 $.ajax({
22 type: “GET”,
23 url: “http://results.com/api/xml/competitions.php”,
24 data: “competitor=1195&season=2016”,
25 dataType: “xml”,
26 success: function(xml) {…}
27 });
28 });
What is happening at 20, 22-24, 25, 26
20 - when document is <strong>fully loaded</strong>
22 - GET -request type
23 - this url
24 - data being sent
25 - data type being sent
26 - if successful get, do this
What is the purpose of I/O thread and Main thread?
Allow for simultaneous processes that don’t block
Comment on the architecture of diagram of under the hood of the Chrome browser - See diagram doc - #8
multi process architectures
What do SPAs separate?
data (both client and server) from content and presentation
In terms of getting data from server into SPA, what is the problem/requirement that is needed?
Need some way of performing HTTP requests concurrently to, and independently of, the user interacting with the application on the browser –> Security, usability, etc
What type of threading does JS have?
JS is single threaded
Summarize XHR / AJAX (5)
✨✨✨
1) Execute HTTP methods programmatically, and in the ‘background’ -> But remember that JavaScript is single threaded.
2) Use JavaScript to issue HTTP requests e.g. HTTP GET, HTTP POST etc.
3) Use XMLHttpRequest
(XHR) JavaScript API
–> Raw JavaScript XHR; or
–> Abstraction of XHR e.g. jQuery’s $.ajax() method, Vue.js’ vue-resource (v2, not v3), axios etc.
4) Setup XHR request
5) Execute the request
In terms of XHR/AJAX, when setting up XHR request, what things do you specify?
1) HTTP <strong>request</strong>
–> Method e.g. GET, etc. including ? query parameters
–> Body e.g. what’s in the HTTP body you’re sending (of anything)
2) Expected HTTP <strong>responses</strong>: what data you want back from the server e.g. JSON, XML etc
3) <strong>Callbacks</strong> for handling the range of HTTP response/s
–> e.g. successful | unsuccessful response
Explain the example of raw XHR - see image doc - #9
1) onreadystatechange –> when we receive response, execute anonymous function
2) this.readyState == 4 –> 4 = done
Other magic numbers:
0 = unsent –> open() but not send()
1 = opened –> opened() and send()
3 = loading
3) xhttp.open() and xhttp.send() –> open connection and send
Explain the model in image #10
FINISH EXPLANATION
1) request event from application goes to the demultiplexer
Demultiplexer stores event in <strong>non-blocking way</strong>
2) Once demultiplexer done, handling pushes out to Event Queue
3) If some event present, Event Queue passes to event loop
4)
Benefits of concurrency (5)
- decreasing waiting time.
- decreasing response time.
- increases resource utilzation.
- increases efficiency.
- Allows for non-blocking behaviour
Compare concurrency and parallelism
concurrency is the composition of independently executing processes, while parallelism is the simultaneous execution of (possibly related) computations.
🧠 Concurrency is about <strong>dealing</strong> with lots of things at once.
🤹♀️ Parallelism is about <strong>doing</strong> lots of things at once.
Why is the term ‘XMLHttpRequest (XHR)’ now misleading?
1) Can retrieve data other than XML e.g. JSON
2) Works with other protocols, not just HTTP
3) Doesn’t have to be asynchronous… but should be asynchronous and NOT synchronous
Why is it best use an abstraction rather than raw XHR e.g. jQuery’s $.ajax() or a library such as axios for Vue , or fetch API?
Internet Explorer variants do things differently.
e.g. XDomainRequest in Internet Explore 8 and 9
What are the events (and therefore event handlers) for XHR? (8)
1) onloadstart
2) onprogress
3) onabort
4) onerror
5) onload
6) ontimeout
7) onloadend
8) onreadystatechange –> Legacy; deprecated
What is the fetch API?
<strong>Modern interface that allows you to make HTTP requests to servers from web browsers.</strong>
It uses the Promise to deliver more flexible features to make these requests.</strong>
1) ES6 style JS
2) syntactic sugar for promises
3) Native Javascript API introduced in 2017
Give the fetch API pattern for a basic fetch.
See diagrams doc - #11
Give the fetch API pattern for a basic fetch that uses an async function
See diagrams doc - #12
What does CORS stand for?
Cross Origin Resource Sharing
What is the main purpose of CORS?
✨✨✨
🦀 Request resources from different domains/servers.
🦀 <strong>Defines a way in which a browser and server can interact to determine if it is safe to allow cross origin requests.</strong>
🦀 Uses Http headers and relies on the browser to honour the restrictions.
🦀 Prevents people from drawing lots of info from resources they that aren’t allowed to
Explain the everyday and legitimate cross-origin resource request diagram –> See diagram #13
TODO
1) Browser sends an HTTP request to first.com to retrieve Resource A’s HTML
2) The server retrieves Resource A and delivers it to the browser in an HTTP response
3) The browser then sends another HTTP request to another address second.com to get Resource B’s image
4) The server retrieves this resource and delivers it to the browser in an HTTP response
What is a common practice for many web pages (web applications) to do in terms of loading resources?
Load resources from separate <strong>domains</strong> –> CSS stylesheets, images, frames, video