<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>EMS</title>
<style type="text/css">
#ems{
position: absolute;
left: 50%;
width: 400px;
height: auto;
margin-left: -200px;
background: lightGreen;
text-align: center;
min-height: 50px;
height: 500px;
overflow-y: scroll;
overflow-x: hidden;
}
#ems input{
height: 25px;
margin-top: 10px;
}
#list{
background: lightBlue;
}
#list li{
text-align: left;
}
</style>
</head>
<body>
<div id="ems">
<div>
<input type="text" id="code">
<input type="button" value="查询" id="btn">
</div>
<div id="list">
</div>
</div>
<script type="text/javascript">
//First step: Create xhr object var xhr = null;if(window.XMLHttpRequest){//Standard browserxhr = new XMLHttpRequest();}else{xhr = new ActiveXObject('Microsoft.XMLHTTP');}//Step 2 : Prepare some configuration parameters before sendingvar type = data.type == 'get'?'get':'post';var url = '';if(data.url){url = data.url;if(type == 'get'){url = "?" data. data "&_t=" new Date().getTime();}}var flag = data.asyn == 'true'?'true': 'false';xhr.open(type,url,flag);
##window.onload = function(){
//Register button click event
var btn = document.getElementById('btn ');
var code = document.getElementById('code').value;
btn.onclick = function(){
var param = {
url:'07ems.php?code=' code,
type:'get',
dataType:'json',
success:function(data ){
if(data.status == 0){
var list = data.data.info.context;
var tag = '';
for(var i=0;i<list.length;i ){
var item = list[i];
var desc = item.desc;
var t = new Date();
t.setTime(item.time '000');
var str = t.getFullYear() 'Year' (t.getMonth( ) 1) 'month' t.getDay() 'day' (t.getHours()>12?' afternoon':' morning') t.getHours() ':' t.getMinutes() ':' t .getSeconds();
## tag = '<li><div>' str '</div><div>' item.desc '< /div></li>';
}
var ul = document.createElement('ul') ;
ul.innerHTML = tag;
document.getElementById('list').appendChild(ul);
}else {
var info = data.msg;
document.getElementById('list').innerHTML = info;
}
},failure:function () {
alert(12);
}
};
ajax(param);
}
}
//Exercise questions: Use encapsulated methods to implement Yesterday’s EMS query
</script>
</body>
</html>
ajax_json.js
client: File name demo.html
server: File name test_json.php