#<元字元集=“UTF-8”>
<標題>EMS標題>
<樣式類型=“text/css”>
#ems{
位置:絕對;
左: 50%;
寬度:400px;
高:自動;
左邊距:-200px;
背景: lightGreen;
文字對齊:居中;
最小高度:50px;
高度:500px;
overflow-y:捲動;
##overflow-x: 隱藏; }##ems 輸入{高度: 25px;margin-top: 10px;##}
##list{
background: lightBlue;
}
#list li{
text -對齊:左;
}
風格>
頭>
#< ;body>
<輸入類型="text" id="code">
<輸入類型=“按鈕”值=“查詢” id=“btn”>
<script type="text/javascript">
#function ajax(data){
##//data= {data:"",dataType:"xml/json",type:"get/post",url:"",asyn:"true/false",success:function(){},failure:function(){} }//第一步:建立xhr物件var xhr = null;if(window.XMLHttpRequest){//標準的瀏覽器xhr = new XMLHttpRequest();}else{xhr = new ActiveXObject('Microsoft.XMLHTTP');}#//第二步:準備發送前的一些配置參數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);
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() '年' (t.getMonth(
#var str = t.getFullYear() '年' (t.getMonth(
#var str = t.getFullYear()' ) 1) '月' t.getDay() '日' (t.getHours()>12?' 下午':' 上午') 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);
}
#}
//練習題:用封裝好的方法去實現昨天的EMS查詢
</script>
</body>
</html>
PHP中文网2017-07-21 10:33:28
ajax_json.js
function ajax(){ try{ this.xmlHttp = new XMLHttpRequest(); }catch(e){ try{ this.xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try{ this.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ alert("your web explorer not support AJAX"); return false; } } } } ajax.prototype.doajax=function(){ var target,func,var_s,count,all_c,i,xmlHttp,appstr,obj; target=arguments[arguments.length-2]; func=arguments[arguments.length-1]; var_s=""; count=0; all_c=0; for(i=0;i<arguments.length-2;i++){ if(count==0){ count=1; if(all_c==0){ all_c=1; var_s+=arguments[i]; }else{ var_s+="&"+arguments[i]; } }else{ count=0; var_s+=("="+escape(replaceall(arguments[i],"+","#add#"))); } } xmlHttp=this.xmlHttp; xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==4 && xmlHttp.status==200){ appstr=unescape(unescape(xmlHttp.responseText)); obj=eval('('+appstr+')'); eval(func+"(obj);"); } }; xmlHttp.open('POST',target,true); xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlHttp.send(var_s); }; function replaceall(str,from,to){ while(str.indexOf(from)>=0){ str=str.replace(from,to); } return(str); }; function aolk(olkt){ return replaceall(replaceall(olkt,"#brn#","\n"),"#brr#","\r"); }
client: 檔案名稱demo.html
<script src="ajax_json.js"></script> <script> function aa(pp){ alert(pp.linkman); alert(aolk(pp.password[0].me)); alert(pp.password[1].you); } </script> <textarea id=user></textarea><br> <input type=button value="1111" onclick='$=new ajax();$.doajax("nm",document.all.user.value,"test_json.php","aa");'>
server: 檔案名稱test_json.php
<?php function escape($str) { preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r); $ar = $r[0]; foreach($ar as $k=>$v) { if(ord($v[0]) < 128) $ar[$k] = rawurlencode($v); else $ar[$k] = "%u".bin2hex(iconv("GB2312","UCS-2",$v)); } return join("",$ar); } function unescape($str) { $str = rawurldecode($str); preg_match_all("/(?:%u.{4})|.+/",$str,$r); $ar = $r[0]; foreach($ar as $k=>$v) { if(substr($v,0,2) == "%u" && strlen($v) == 6) $ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,-4))); } return join("",$ar); } function olk($olkt){ return str_replace("#add#","+",str_replace("\r","#brr#",str_replace("\n","#brn#",str_replace('"','\"',$olkt)))); } $nm=unescape(stripslashes($_POST["nm"])); echo escape('{"linkman":"1111","password":[{"me":"'.olk($nm).'"},{"you":"hello"}]}');