-
Notifications
You must be signed in to change notification settings - Fork 1
/
reg.js
49 lines (38 loc) · 1.3 KB
/
reg.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
var f = document.forms[0] //获取注册表单
f.addEventListener('submit',function(e){
e.preventDefault() //阻止表单提交的默认行为
//通过Ajax将表单的数据发送给服务器
// 1 实例化
var xhr = new XMLHttpRequest()
// 2 监听 readyState == 4
xhr.onreadystatechange = function(){
if(xhr.readyState==4 && xhr.status==200)
{
//接收服务器的响应数据
var json_str = xhr.responseText
var data = JSON.parse(json_str)
//判断接口返回的状态码
if(data.errno==0){ //成功
alert("注册成功")
window.location.href = "login.html"
}else{
alert(data.msg)
}
}
}
// 3 open
xhr.open("POST","reg.php")
// 4 send
var inputs = f.querySelectorAll("input") //获取所有input
var form_param = "" //拼接要发送的参数
for(var i=0;i<inputs.length;i++)
{
if(inputs[i].getAttribute("name")===null ){
continue;
}
form_param += inputs[i].getAttribute("name") + "=" + inputs[i].value + "&"
}
//设置 http请求头
xhr.setRequestHeader("content-type","application/x-www-form-urlencoded")
xhr.send(form_param)
})