<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>EMS<
type>EMS<="text/css">
#ems{
kedudukan: mutlak;
kiri: 50%;
lebar: 400px;
ketinggian: auto;
20 belakang-belakang
;-margin-latar belakang: LightGreen;
text-align: center;
min-height: 50px;
height: 500px;
overflow-y: scroll;
overflow-x: hidden;
s input{}
tinggi: 25px;margin-atas: 10px;}#senarai{latar belakang: Biru muda;}#senarai kiri{; </style></head>
="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';
//Langkah pertama: Cipta objek xhr
var xhr = null;
if(window.XMLHttpRequest){//Pelayar standard
=XMLHttpRequest();
}else{
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
//Langkah 2: Sediakan beberapa parameter konfigurasi sebelum menghantar
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':' false';
xhr.open(type,url,flag);
//Langkah 3: Laksanakan tindakan yang dihantar
if(type == 'get'){
xhr.send(null) ;
}lain jika(taip == 'siaran'){
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr. ;
}
//Langkah 4: Tentukan fungsi panggil balik
xhr.onreadystatechange = function(){
if(this.readyState == 4){
0if(ini.0
if) ){jika(jenis data.kejayaan == 'fungsi'){var d = data.dataType == 'xml'?xhr.responseXML:xhr.responseText;data.success (d); }}lain{jika(jenis data.kegagalan == 'fungsi'){data.failure();}}}}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 = baharu Tarikh (); 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.getlist. appendChild(ul);
}lain{
var info = data.msg;
document.getElementById('list').innerHTML = info;
}
}
}
failure:function () {
alert(12);
}
};
ajax(param);
}
}
//Melaksanakan kaedah latihan Semalam pertanyaan
</skrip>
</badan>
</html>
ajax_json.js
klien: Nama fail demo.html
pelayan: Nama fail test_json.php