forked from billmian/socketio-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.html
52 lines (51 loc) · 1.53 KB
/
login.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="login.css" />
<title>聊天室登陆</title>
</head>
<body>
<div class="container">
<div>这里设置你的聊天名称:</div>
<input id="userName-input" />
<div class="login">进入聊天室</div>
</div>
<div class="container">
<div id="error"></div>
</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script>
const loginButton = document.querySelector(".login");
const userNameInput = document.querySelector("#userName-input");
const error = document.querySelector("#error");
loginButton.addEventListener("click", (e) => {
$.ajax({
asynchronous: false,
type: "post",
url: "/userName",
contentType: "application/json",
data: JSON.stringify({
userName: userNameInput.value,
}),
})
.done((res, status, xhr) => {
if (xhr.status === 200) {
window.location.href = "http://localhost:3000/index";
}
})
.fail((res, status, xhr) => {
console.log(res.status);
if (res.status === 400) {
error.innerHTML = "名字不能为空";
}
if (res.status === 403) {
error.innerHTML = "名字重复";
}
});
});
//"application/x-www-form-urlencoded"
</script>
</html>