From 6433eaccbfcfe1472b94e3c8408ab125779f908a Mon Sep 17 00:00:00 2001 From: kaotisk Date: Tue, 5 Sep 2023 02:22:53 +0300 Subject: Moving from Fetch to XMLHttpRequest --- src/js/arching-kaos-fetch.js | 49 +++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 12 deletions(-) (limited to 'src/js') diff --git a/src/js/arching-kaos-fetch.js b/src/js/arching-kaos-fetch.js index 91e2419..bc982f9 100644 --- a/src/js/arching-kaos-fetch.js +++ b/src/js/arching-kaos-fetch.js @@ -1,18 +1,43 @@ function archingKaosFetchJSON( url, callback ){ - fetch(url, { - method:'GET', - headers:{ - Accept: 'application/json' - } - }).then(response=>{ - if(response.ok){ - response.json().then(json=>{ - callback(json); - }) +// fetch(url, { +// method:'GET', +// headers:{ +// Accept: 'application/json' +// } +// }).then(response=>{ +// if(response.ok){ +// response.json().then(json=>{ +// callback(json); +// }) +// } else { +// if (DEBUG) console.log(e); +// } +// }).catch( e=>{ +// if (DEBUG) console.log(e); +// }) + const request = new XMLHttpRequest(); + request.addEventListener("load", ()=>{ + console.log(request.response); + var json = JSON.parse(request.response); + callback(json); + }); + request.addEventListener("error", ()=>{ + console.log("An error occured."); + }); + request.addEventListener("progress", (event)=>{ + console.log(request.response); + if (event.lengthComputable && progressPlaceholder){ + progressPlaceholder = (event.loaded / event.total) * 100; } else { - if (DEBUG) console.log(e); + progressPlaceholder = 0; } - }) + }); + request.addEventListener("abort", ()=>{ + console.log("Request aborted."); + }); + request.open("GET", url); + request.send(); + } async function archingKaosFetchText( url, callback ){ -- cgit v1.2.3