Advertisement
tosip

fetch_api

Dec 3rd, 2022
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const api_url = 'https://www.themealdb.com/api/json/v1/1/categories.php';
  2.  
  3. //defining an async function
  4.  
  5. // async function getApi(url) {
  6. //   //storing the response
  7. //   const response = await fetch(url);
  8.  
  9. //   //storing data in form of JSON
  10. //   let data = await response.json();
  11. //   console.log(data.categories);
  12. //   show(data);
  13. // }
  14. const get_data = (req, res) => {
  15.   fetch(api_url)
  16.     .then((res) => res.json())
  17.     .then((data) => {
  18.       show(data);
  19.       console.log(data.categories);
  20.     });
  21. };
  22.  
  23. const search_data = (req, res) => {
  24.    
  25. }
  26.  
  27. function show(data) {
  28.   let tab = `<tr>
  29.           <th>Id</th>
  30.           <th>Category</th>
  31.           <th>CategoryThumb</th>
  32.           <th>Description</th>
  33.          </tr>`;
  34.  
  35.   // Loop to access all rows
  36.   for (let d of data.categories) {
  37.     tab += `<tr>
  38.     <td>${d.idCategory} </td>
  39.     <td>${d.strCategory}</td>
  40.     <td>${d.strCategoryThumb}</td>
  41.     <td>${d.strCategoryDescription}</td>          
  42. </tr>`;
  43.   }
  44.   // Setting innerHTML as tab variable
  45.   document.getElementById('meals').innerHTML = tab;
  46. }
  47.  
  48. get_data(api_url);
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement