<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>EMS</title>
<스타일 유형 ="text/css">
#ems{
위치: 절대;
왼쪽: 50%;
너비: 400px;
높이: 자동;
margin-왼쪽: -200px;
배경 : lightGreen;
text-align: center;
min-height: 50px;
height: 500px;
overflow-y: 스크롤;
overflow-x: Hidden;
}
#ems 입력{
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:{사용자 이름:123,password:456 }
//data = 'username=123&password=456';
//1단계: xhr 객체 생성
var xhr = null;
if(window.XMLHttpRequest){//표준 브라우저
xhr = new XMLHttpRequest();
}else{
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
//2단계: 보내기 전에 일부 구성 매개변수 준비
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 플래그 = data.asyn == 'true':' false';
xhr.open(type,url,flag);
//3단계: 전송된 작업 실행
if(type == 'get'){
xhr.send(null) ;
}else if(type == 'post'){
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr. ;
}
//4단계: 콜백 함수 지정
xhr.onreadystatechange = function(){
if(this.readyState == 4){
if( this.status == 200 ){
if(typeof data.success == '함수'){
var d = data.dataType == 'xml'?xhr.responseXML:xhr.responseText;
data.success (d);
}
}else{
if(data.failure 유형 == '함수'){
data.failure();
}
}
}
}
}
window.onload = function(){
//버튼 클릭 이벤트 등록
var btn = document.getElementById ('btn') ;
var code = 문서 .getElementById('코드').value;
btn.onclick = function(){
var param = {
url:'07ems.php?code='+code,
유형:'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 = 새 날짜();
t. setTime(item.time+'000');
var str = t.getFullYear()+'연도'+(t.getMonth()+1)+'월'+t.getDay( )+'일'+( t.getHours()>12?' PM':' AM')+t.getHours()%12+':'+t.getMinutes()+':'+t.getSeconds ();
태그 += '<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: 파일 이름 데모.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"}]}');