Kawiesh

Fetch

Mar 17th, 2022
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function _fetch(url){
  2. const response= await fetch(url);
  3. if (!response.ok){
  4. const message= `Error: ${response.status}`;
  5. throw new Error(message);
  6. }
  7.  
  8. const data= await response.clone().json().catch(()=> response.text());
  9.  
  10. return data;
  11. }
  12.  
  13.  
  14.  
  15.  
  16. //Calling the function
  17. let url= "https://kawiesh.duckdns.org";
  18. _fetch(url)
  19. .then(d=>{
  20. let doc= new DOMParser().parseFromString(d,"text/html");
  21. alert(d);
  22. //Do something with the received data
  23. })
  24. .catch(x=> alert(x.message));
Advertisement
Add Comment
Please, Sign In to add comment