Advertisement
agel122

Untitled

Oct 8th, 2021
1,046
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.70 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5. </head>
  6.  
  7. <body>
  8.     <p><button id="getdata1">It's my button to get one data</button></p>
  9.     And here a data itself:
  10.     <p><textarea id="datahere1"></textarea></p>
  11.     <p><button id="getdata2">It's my button to get all data</button></p>
  12.     And here a data itself:
  13.     <p><textarea id="datahere2"></textarea></p>
  14.     <p>
  15.     <form id="myform">
  16.         firstname:<input type="text" name="firstname">
  17.         secondname:<input type="text" name="secondname">
  18.         <input type="submit">
  19.     </form>
  20.     </p>
  21.     <script>
  22.         async function getjson(myurl, curmethod) {
  23.             let response = await fetch(myurl, {
  24.                 mode: "no-cors",
  25.                 method: curmethod,
  26.                 headers: {
  27.                     "Accept": "application/json"
  28.                 }
  29.             });
  30.             let body = await response.json();
  31.             return body
  32.         }
  33.         //getjson();
  34.  
  35.         let myurl1 = "http://127.0.0.1:8000/myfamily/1/";
  36.         let myurl2 = "http://127.0.0.1:8000/myfamily/";
  37.         let buttontoget1 = document.getElementById("getdata1");
  38.         let buttontoget2 = document.getElementById("getdata2");
  39.  
  40.  
  41.         buttontoget1.onclick = function () {
  42.             getjson(myurl1, "GET").then(result => {
  43.                 document.getElementById("datahere1").value = `${result["firstname"]} ${result["secondname"]}`
  44.             })
  45.         }
  46.         buttontoget2.onclick = function () {
  47.             getjson(myurl2, "GET").then(result => {
  48.                 let data = '';
  49.                 for (let record of result) {
  50.                     data += (`${record["firstname"]} ${record["secondname"]}` + '\n')
  51.                 }
  52.                 document.getElementById("datahere2").value = data
  53.             })
  54.         }
  55.  
  56.         let testpost = {
  57.             firstname: 'koteyka',
  58.             secondname: 'kotova'
  59.         };
  60.  
  61.         document.getElementById("myform").onsubmit = async (e) => {
  62.             e.preventDefault();
  63.             //let formData = await new FormData(document.getElementById("myform"));
  64.             let response = await fetch(myurl2, {
  65.                 mode: "no-cors",
  66.                 method: "POST",
  67.                 headers: {
  68.                     'Accept': 'application/json',
  69.                     'Content-Type': 'application/json;charset=utf-8'
  70.                 },
  71.                 //body: JSON.stringify(Object.fromEntries(formData))
  72.                 body: JSON.stringify(testpost)
  73.             });
  74.             let result = await response.json();
  75.             alert(result.message);
  76.             //let result = await response.json();
  77.             //alert(result.message);
  78.         }
  79.     </script>
  80. </body>
  81.  
  82. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement