所有ajax
我是聋人,不知道老师讲了什么?感觉老师讲了很多
我是聋人,不知道老师讲了什么?感觉老师讲了很多
2017-01-05
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
body,label,h1,input,option,select,button{
font-size:30px;
line-height:1.8;
}
</style>
</head>
<body>
<h1>员工查询</h1>
<label>请输入员工编号:</label>
<input type="text" id="keyword"/>
<button id="search">查询</button>
<p id="searchResult"></p>
<h1>员工创建</h1>
<label>请输入员工姓名:</label>
<input type="text" id="staffName"/><br/>
<label>请输入员工编号:</label>
<input type="txet" id="staffNumber"/><br/>
<label>请选择员工性别:</label>
<select id="staffSex">
<option>男</option>
<option>女</option>
</select>
<br/>
<label>请输入员工职位:</label>
<input type="text" id="staffJob"/><br/>
<button id="save">保存</button>
<p id="createResult"></p>
<script type="text/javascript">
document.getElementById("search").onclick=function(){
//发送Ajax查询请求并处理
var request = new XMLHttpRequest();
request.open("GET","service.php?number="+document.getElementById("keyword").value);
request.send();
//获取服务器readyState的属性
request.onreadystatechange=function(){
if(request.readyState === 4){
//判断请求是否成功
if(request.status === 200){
document.getElementById("searchResult").innerHTML = request.responseText;
}else{
alert("发生错误!"+request.status)
}
}
}
}
document.getElementById("save").onclick = function(){
//发送Ajax保存处理
var request = new XMLHttpRequest();
request.open("POST","service.php");
var data = "name="+document.getElementById("staffName").value
+"&number="+document.getElementById("staffNumber").value
+"&sex="+document.getElementById("staffSex").value
+"&job="+document.getElementById("staffJob").value;
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
request.send(data);
request.onreadystatechange = function(){
if(request.readyState === 4){
if(request.status === 200){
document.getElementById("createResult").innerHTML = request.responseText;
}else{
alert("发生错误!"+request.status);
}
}
}
}
</script>
</body>
</html>
举报