form.html
2.1 KB
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
53
54
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mukho - register form</title>
</head>
<body>
<form action="/register/form" method="post">
<h2>Hello. It's me mukho.</h2>
<a href="http://localhost:3000/">main</a>
<a href="http://localhost:3000/about">about</a>
<a href="http://localhost:3000/info">info</a><br/>
id : <input type="text" name="ID" required minlength='1' maxlength='20' placeholder="ID"><br/>
password : <input type="password" name="password" required minlength='1' maxlength='20' placeholder="Password"><br/> <!-- type password는 입력 값을 *로 표시함-->
<input type="submit">
</form>
<button class="ajaxsend">AJAXsend</button>
<div class="result"></div>
<script>
document.querySelector('.ajaxsend').addEventListener('click', function(){
var inputID = document.forms[0].elements[0].value;
var inputPW = document.forms[0].elements[1].value;
var inputdata = {'ID' : inputID , 'password' : inputPW};
sendAjax('http://localhost:3000/register/login', inputdata);
})
function sendAjax(url, data){
var data = {'ID' : data.ID, 'password' : data.password};
data = JSON.stringify(data);
var xhr = new XMLHttpRequest();
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type', "application/json"); // 서버로 보낼 때 json type으로 보내겠다.
xhr.send(data);
xhr.addEventListener('load', function(){
var result = JSON.parse(xhr.responseText);
var resultDiv = document.querySelector(".result");
// 웹 반응
if(result.result == "ok") resultDiv.innerHTML = result.ID;
else if(result.result == 'pw err') resultDiv.innerHTML = "password error";
else resultDiv.innerHTML = "";
});
};
</script>
</body>
</html>