Vadym Hlushko 3 роки тому
батько
коміт
1c5368f618
1 змінених файлів з 29 додано та 19 видалено
  1. 29 19
      hw14/main.js

+ 29 - 19
hw14/main.js

@@ -1,22 +1,24 @@
-function jsonPost(url, data)
-{
-    return new Promise((resolve, reject) => {
-        var x = new XMLHttpRequest();   
-        x.onerror = () => reject(new Error('jsonPost failed'))
-        //x.setRequestHeader('Content-Type', 'application/json');
-        x.open("POST", url, true);
-        x.send(JSON.stringify(data))
-
-        x.onreadystatechange = () => {
-            if (x.readyState == XMLHttpRequest.DONE && x.status == 200){
-                resolve(JSON.parse(x.responseText))
-            }
-            else if (x.status != 200){
-                reject(new Error('status is not 200'))
-            }
-        }
-    })
-}
+// function jsonPost(url, data)
+// {
+//     return new Promise((resolve, reject) => {
+//         var x = new XMLHttpRequest();   
+//         x.onerror = () => reject(new Error('jsonPost failed'))
+//         //x.setRequestHeader('Content-Type', 'application/json');
+//         x.open("POST", url, true);
+//         x.send(JSON.stringify(data))
+
+//         x.onreadystatechange = () => {
+//             if (x.readyState == XMLHttpRequest.DONE && x.status == 200){
+//                 resolve(JSON.parse(x.responseText))
+//             }
+//             else if (x.status != 200){
+//                 reject(new Error('status is not 200'))
+//             }
+//         }
+//     })
+// }
+
+
 
 let sendBtn = document.getElementById('sendButton')
 sendBtn.innerHTML = "Send"
@@ -72,6 +74,14 @@ sendBtn.style.fontSize = '20px'
 // addMsg()
 
 //async
+async function jsonPost(url, data)
+{
+    let res = await fetch(url, {
+        method: 'POST', 
+        body: JSON.stringify(data) 
+      });
+      return await res.json();}
+
 function delay(ms) {
     return new Promise(resolve => setTimeout(resolve, ms));
   }