nanorocks

JavaScript API Calls example

May 31st, 2020
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Document</title>
  7. </head>
  8. <body>
  9.     <div id="load">
  10.  
  11.     </div>
  12.     <script>
  13.         var data = null;
  14.  
  15.         function getPostsXhR() {
  16.             var xhr = new XMLHttpRequest();
  17.  
  18.             xhr.addEventListener('readystatechange', function(){
  19.                 if(this.readyState === 4 && this.status === 200){
  20.                      console.log(this.responseText);  
  21.                 }else{
  22.                     console.log("THere is an error", "Status code: " + this.status);
  23.                 }
  24.             });
  25.  
  26.             xhr.open("GET", "https://jsonplaceholder.typicode.com/posts");
  27.             xhr.send(data);
  28.         }
  29.  
  30.         function getPostsPromises() {
  31.             return new Promise(function(resolve, reject){
  32.  
  33.                 let xhr = new XMLHttpRequest();
  34.  
  35.                 xhr.onreadystatechange = function(e){
  36.                     if(this.readyState === 4 && this.status == 200){  
  37.                         resolve(this.response);
  38.                     }else if(this.readyState === 4 && this.status == 200){
  39.                         console.log("Status:", this.status);
  40.                         reject(this.status)
  41.                     }
  42.                 };
  43.  
  44.                 xhr.open("GET", "https://jsonplaceholder.typicode.com/posts", true);
  45.                 xhr.responseType = 'json';
  46.                 xhr.send();
  47.             });
  48.         }
  49.    
  50.         function getPostsUsingFetchOnly(){
  51.  
  52.             var fetchData = {
  53.                 method: 'GET',
  54.                 headers: new Headers()
  55.             }
  56.             fetch("https://jsonplaceholder.typicode.com/posts", fetchData)
  57.             .then(function(response){
  58.                 return response.json();
  59.             }).then(function(data){
  60.                 console.log(data);
  61.             }).catch(function(e){
  62.                 console.log("err: ", e);
  63.             });
  64.         }
  65.  
  66.         window.onload = function(){
  67.  
  68.             // getPostsPromises().then(function(data){
  69.             //     console.log("Data: ", data);
  70.             // }).catch(function(err){
  71.             //     console.log("Error: ", err);
  72.             // }).finally(function(){
  73.             //     console.log('Finish!!!');
  74.             // });
  75.  
  76.             getPostsUsingFetchOnly();
  77.  
  78.         }
  79.     </script>
  80. </body>
  81. </html>
Add Comment
Please, Sign In to add comment