-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax2.js
82 lines (73 loc) · 2.49 KB
/
ajax2.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// var send = document.querySelector('.send');
// send.addEventListener('click',signup,false);
// function signup(){
// var emailStr = document.querySelector('.account').value;
// var passwordStr = document.querySelector('.password').value;
// var account = {};
// account.email = emailStr;
// account.password =passwordStr;
// var xhr = new XMLHttpRequest();
// xhr.open('post','https://hexschool-tutorial.herokuapp.com/api/signup',true);
// xhr.setRequestHeader('Content-type','application/json');
// var data = JSON.stringify(account);
// xhr.send(data);
// xhr.onload = function(){
// var callbackData = JSON.parse(xhr.responseText);
// console.log(callbackData);
// var veriStr = callbackData.message;
// if(veriStr =="帳號註冊成功"){
// alert('帳號註冊成功');
// }else{
// alert('帳號註冊失敗');
// }
// }
// }
// ---------
var signupbtn = document.querySelector('.signup');
var signinbtn = document.querySelector('.signin');
signupbtn.addEventListener('click',signup,false);
signinbtn.addEventListener('click',signin,false);
function signup(){
var inor = document.querySelector(".inor");
var emailStr = document.querySelector('.account').value;
var passwordStr = document.querySelector('.password').value;
var account = {};
account.email = emailStr;
account.password =passwordStr;
var xhr = new XMLHttpRequest();
xhr.open('post','https://hexschool-tutorial.herokuapp.com/api/signup',true);
xhr.setRequestHeader('Content-type','application/json');
var data = JSON.stringify(account);
xhr.send(data);
xhr.onload = function(){
var callbackData = JSON.parse(xhr.responseText);
var veriStr = callbackData.message;
if(veriStr == "帳號註冊成功"){
inor.textContent = "帳號註冊成功";
}else{
inor.textContent = veriStr;
}
};
}
function signin(){
var inor = document.querySelector(".inor");
var emailStr = document.querySelector('.account').value;
var passwordStr = document.querySelector('.password').value;
var account = {};
account.email = emailStr;
account.password =passwordStr;
var xhr = new XMLHttpRequest();
xhr.open('post','https://hexschool-tutorial.herokuapp.com/api/signup',true);
xhr.setRequestHeader('Content-type','application/json');
var data = JSON.stringify(account);
xhr.send(data);
xhr.onload = function(){
var callbackData = JSON.parse(xhr.responseText);
var veriStr = callbackData.message;
if(veriStr == "登入成功"){
inor.textContent = "帳號登入成功";
}else{
inor.textContent = veriStr;
}
};
}