Web APIs
Last updated
Last updated
Fetch: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
a better version of XHR
it returns Promise: fetch(
https://api.github.com/users/${this.name}`) .then(resp => resp.json());`
4xx and 5xx are not considerd as error so it's in then
living standard: safari and IE not support it
// GET
fetch('./api/some.json')
.then(
function(response) {
if (response.status !== 200) {
console.log('Looks like there was a problem. Status Code: ' +
XMLHttpRequest:
// JSON
var req = new XMLHttpRequest();
req.responseType = 'json';
req.open('GET', url, true);
req.onload = function() {
var jsonResponse = req.response;
// do something with jsonResponse
};
req.send(null);
// JSON
function reqListener() {
var data = JSON.parse(this.responseText);
console.log(data);
}
function reqError(err) {
console.log('Fetch Error :-S', err);
}
var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.onerror = reqError;
oReq.open('get', './api/some.json', true);
oReq.send();
background thread (other than UI thread)
cannot access DOM
different context window
Proxy between browser and network. Handles network requests. Thus enabling offline experience.
it's web worker and more
cannot access DOM
Communicate from browser to server.
Socket.IO: A long polling/WebSocket based third party transfer protocol for Node.js.