Spaces:
Sleeping
Sleeping
Update static/scripts/login.js
Browse files- static/scripts/login.js +23 -10
static/scripts/login.js
CHANGED
|
@@ -7,18 +7,31 @@ function login() {
|
|
| 7 |
method: 'POST',
|
| 8 |
body: JSON.stringify({"username": userInput.value, "password": passInput.value})
|
| 9 |
})
|
| 10 |
-
.then(
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
alert(`Error - ${data.message}`);
|
| 14 |
-
return;
|
| 15 |
-
} else if (!response.ok) {
|
| 16 |
-
return;
|
| 17 |
-
} else if (response.ok) {
|
| 18 |
-
alert("Successful")
|
| 19 |
-
// window.location.reload();
|
| 20 |
}
|
|
|
|
| 21 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
}
|
| 23 |
|
| 24 |
document.addEventListener("DOMContentLoaded", (event) => {
|
|
|
|
| 7 |
method: 'POST',
|
| 8 |
body: JSON.stringify({"username": userInput.value, "password": passInput.value})
|
| 9 |
})
|
| 10 |
+
.then(response => {
|
| 11 |
+
if (!response.ok) {
|
| 12 |
+
throw response; // throw the entire response to read it in catch block;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
}
|
| 14 |
+
return response.json();
|
| 15 |
})
|
| 16 |
+
.then(data => {
|
| 17 |
+
console.log("Success - ", data);
|
| 18 |
+
})
|
| 19 |
+
.catch(errorResponse => {
|
| 20 |
+
if (errorResponse instanceof Response) {
|
| 21 |
+
errorResponse.json()
|
| 22 |
+
.then(errorData => {
|
| 23 |
+
console.error('Error from server - ', errorData);
|
| 24 |
+
})
|
| 25 |
+
.catch(jsonParseError => {
|
| 26 |
+
console.error("Error parsing error from server", jsonParseError)
|
| 27 |
+
errorResponse.text().then(textError => {
|
| 28 |
+
console.error("Error as plain text - ", textError);
|
| 29 |
+
});
|
| 30 |
+
});
|
| 31 |
+
} else {
|
| 32 |
+
console.error("Unhandled error (network or other) - ", errorResponse);
|
| 33 |
+
}
|
| 34 |
+
});
|
| 35 |
}
|
| 36 |
|
| 37 |
document.addEventListener("DOMContentLoaded", (event) => {
|