May 2017
Intermediate to advanced
448 pages
10h 10m
English
Another recent addition to JavaScript is the fetch() function. This is a more flexible alternative to XMLHttpRequest. For example, it's much easier to use fetch() when making cross-domain requests, or when you need to tweak specific HTTP header values. Let's implement the G entries using fetch():
$(() => { $('#letter-g a') .click((e) => { e.preventDefault(); fetch('/g') .then(resp => resp.json()) .then(data => { const html = data.reduce((result, entry) => ` ${result} <div class="entry"> <h3 class="term">${entry.term}</h3> <div class="part">${entry.part}</div> <div class="definition"> ${entry.definition} ${formatQuote(entry)} </div> </div> `, ''); $('#dictionary') .html(html); }); });});
The fetch() function returns ...
Read now
Unlock full access