<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>EMS</title>
<style type ="text/css">
#ems{
Position: absolut;
links: 50 %;
Breite: 400px;
Höhe: automatisch;
Rand links: -200px;
Hintergrund : hellgrün;
text-align: zentriert;
min-height: 50px;
height: 500px;
overflow-y: scrollen;
overflow-x: versteckt;
}
#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">
function ajax(data){
//data={data:"",dataType:"xml/json",type:"get /post",url:"",asyn:"true/false",success:function(){},failure:function(){}}
//data:{username:123,password:456 }
//data = 'username=123&password=456';
//Schritt eins: XHR-Objekt erstellen
var xhr = null;
if(window.XMLHttpRequest){//Standardbrowser
xhr = neu XMLHttpRequest();
}else{
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
//Schritt 2: Vor dem Senden einige Konfigurationsparameter vorbereiten
var 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);
//Schritt 3: Gesendete Aktion ausführen
if(type == 'get'){
xhr.send(null) ;
}else if(type == 'post'){
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr send(data.data) ;
}
//Schritt 4: Geben Sie die Rückruffunktion an
xhr.onreadystatechange = function(){
if(this.readyState == 4){
if( this.status == 200 ){
if(typeof data.success == 'function'){
var d = data.dataType == 'xml'?xhr.responseXML:xhr.responseText;
data.success (d);
}
}else{
if(typeof data.failure == 'function'){
data.failure();
}
}
}
}
}
window.onload = function(){
//Schaltflächenklickereignis registrieren
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?' PM':' AM')+t.getHours()%12+':'+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;
}
}, fail:function () {
alert(12);
}
};
ajax(param);
}
}
//Übungsfragen: Verwenden Sie gekapselte Methoden, um das EMS von gestern zu implementieren Anfrage
</script>
</body>
</html>
ajax_json.js
client: 文件名demo.html
server: 文件名test_json.php