Vadym Hlushko il y a 3 ans
Parent
commit
1c5368f618
1 fichiers modifiés avec 29 ajouts et 19 suppressions
  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));
   }