Loading files using the standard Fetch API

In all modern browsers, you can also load external files using the Fetch API. It's the new JavaScript standard for loading files asynchronously, and we will be using it in all examples that load external files in this book, but it may not work in some older browsers. In that case, you should revert to standard JavaScript or JQuery.

The fetch() command is a reactive method based on JavaScript promises. A basic fetch request is shown as follows (Examples/example-12-fetch.html):

fetch('Data/sample.csv')
.then(response => response.text())
.then(function(text) {
parse(text);
});

You can also parse JSON files using fetch():

fetch('Data/sample.json')
.then(response => response.json())
.then(function(object) {
// use the JavaScript object
});
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset