javascript to fetch data from url code example
Example 1: get data from fetch response html
fetch('/about').then(function (response) {
return response.text();
}).then(function (html) {
var parser = new DOMParser();
var doc = parser.parseFromString(html, 'text/html');
var img = doc.querySelector('img');
console.log(img);
}).catch(function (err) {
console.warn('Something went wrong.', err);
});
Example 2: fetch get data js
var myHeaders = new Headers();
var myInit = { method: 'GET',
headers: myHeaders,
mode: 'cors',
cache: 'default' };
fetch('flowers.jpg',myInit)
.then(function(response) {
return response.blob();
})
.then(function(myBlob) {
var objectURL = URL.createObjectURL(myBlob);
myImage.src = objectURL;
});