asimryu

json api test jsonplaceholder

Aug 5th, 2020
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.73 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>RESTful API</title>
  6.     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.1/css/bootstrap.min.css" integrity="sha384-VCmXjywReHh4PwowAiWNagnWcLhlEJLA5buUprzK8rxFgeH0kww/aWY76TfkUoSX" crossorigin="anonymous">
  7. </head>
  8. <body>
  9.     <div class="container">
  10.         <div class="jumbotron">
  11.             <h1 class="display-4">RESTful API</h1>
  12.             <p class="lead">A simple RESTful API with Fake JSON API</p>
  13.         </div>
  14.         <div class="row" id="posts"></div>
  15.     </div>
  16.  
  17.     <script>
  18.         const getposts = () => {
  19.             fetch('https://jsonplaceholder.typicode.com/posts')
  20.                 .then( (response) => response.json() )
  21.                 .then( (json) => {
  22.                     json.forEach( post => {
  23.                         let card = `
  24.                             <div class='col-sm-4 mb-4'>
  25.                                 <div class='card'>
  26.                                     <div class='card-body'>
  27.                                         <h5 class='card-title'>
  28.                                             ${post.title.substring(0,20)}
  29.                                         </h5>
  30.                                         <p class='card-text'>
  31.                                             ${post.body.substring(0,100)}
  32.                                         </p>
  33.                                         <a href='#' onclick='viewMore(${post.id})'>more</a>
  34.                                         <a href='#' onclick='viewCmts(${post.id})'>comments</a>
  35.                                     </div>
  36.                                 </div>
  37.                             </div>
  38.                             `;
  39.                         document.getElementById('posts').innerHTML += card;
  40.                     } );
  41.                 } );
  42.         }
  43.  
  44.         const viewMore = (id) => {
  45.             fetch(`https://jsonplaceholder.typicode.com/posts/${id}`)
  46.                 .then( (response) => response.json() )
  47.                 .then( (json) => {
  48.                     console.log(json);
  49.                 });
  50.         }
  51.  
  52.         const viewCmts = (id) => {
  53.             fetch(`https://jsonplaceholder.typicode.com/posts/${id}/comments`)
  54.                 .then( (response) => response.json() )
  55.                 .then( (json) => {
  56.                     console.log(json);
  57.                 });
  58.         }
  59.  
  60.         getposts();
  61.     </script>
  62. </body>
  63. </html>
Add Comment
Please, Sign In to add comment