I have a question.
How can I do dispatch fetch's event?
In my code, I added addEventListener
, but I don't know why It doesn't work.
html
<button id="btn">Button</button>
Javascript
var btn = document.getElementById('btn');
window.addEventListener('fetch', function (event) {
console.log("fetch add event listener");
});
btn.addEventListener('click', function (event) {
fetch('https://httpbin.org/get')
.then(data => {console.log(data)})
});
Codepen Link
http://codepen.io/cnaa97/pen/JEogrr
Please advise me what to do.
Below is the MDN reference link.
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Response_objects
I suggest you to use javascript Promise as it is more flexible and efficient to handle response. Please look at the jsfiddle i have created for you https://jsfiddle.net/8j8uwcy7/
HTML
JavaScript
Open the browser console and see the response. You can now extract the necessary data from the API.