ajax.js
function Ajax(recvType){ var ajax = new Object(); //判断是html还是xml ajax.recvType = recvType?recvType.toUpperCase():"HTML"; ajax.targetUrl = ''; ajax.sendString = ''; ajax.resultHandle = ''; ajax.createXMLHTTPRequest = function(){ var request = false; //一般先判断非IE浏览器 //window对象中有XMLHttpRequest存在就是非IE,包括(IE7,IE8) if(window.XMLHttpRequest){ request=new XMLHttpRequest();//非IE以及IE7,IE8浏览器 if(request.overrideMimeType){ request.overrideMimeType("text/xml");//重置mime类型 } //window对象中有ActiveXObject属性存在就是IE浏览器的低版本 }else if(window.ActiveXObject){ var versions=['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Msxml2.XMLHTTP.7.0','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];//各种IE浏览器创建Ajax对象时传递的参数 for(var i=0; i<versions.length; i++){ try{ request=new ActiveXObject(versions[i]);//各个IE浏览器版本的参数不同 if(request){ return request; } }catch(e){ request=false; } } } return request; }; ajax.XMLHttpRequest = ajax.createXMLHTTPRequest(); ajax.processHandle = function(){ if (ajax.XMLHttpRequest.readyState==4) { if (ajax.XMLHttpRequest.status==200) { if (ajax.recvType=="HTML") { ajax.resultHandle(ajax.XMLHttpRequest.responseText); }else if(ajax.recvType=="XML") { ajax.resultHandle(ajax.XMLHttpRequest.responseXML); } } } }; ajax.get = function(targetUrl,resultHandle){ ajax.targetUrl = targetUrl; if (resultHandle!=null) { ajax.XMLHttpRequest.onreadystatechange = ajax.processHandle; ajax.resultHandle = resultHandle; } if (window.XMLHttpRequest) { ajax.XMLHttpRequest.open("get",ajax.targetUrl); ajax.XMLHttpRequest.send(null); }else{ ajax.XMLHttpRequest.open("get",ajax.targetUrl,true); ajax.XMLHttpRequest.send(); } }; ajax.post = function(targetUrl,sendString,resultHandle){ ajax.targetUrl = targetUrl; if (typeof(sendString)=="object") { var str = ""; for (var pro in sendString) { str+=pro+"="+sendString[pro]+"&"; } ajax.sendString=str.substring(0, str.length-1); }else{ ajax.sendString=sendString; } if (resultHandle!=null) { ajax.XMLHttpRequest.onreadystatechange = ajax.processHandle; ajax.resultHandle = resultHandle; } ajax.XMLHttpRequest.open("post",ajax.targetUrl,true); ajax.XMLHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); ajax.XMLHttpRequest.send(ajax.sendString); }; return ajax; }
ajaxObj.php
<?php /** * 自定义ajax对象,简化ajax应用 * * var arr = ["aa","bb","cc","dd"]; * var obj = {name:"sa",age:10,sex:"man"}; * alert(obj.name); */ ?> <script src="ajax.js"></script> <script> var ajax = Ajax(); //ajax.get("serverget.php?username=zhangsan&[email protected]×tamp="+Math.random(),function(data){ // alert(data);}); //ajax.post("serverpost.php","username=list&[email protected]×tamp="+Math.random(),function(data){ // alert(data);}); ajax.post("serverpost.php",{username:"sans",email:"sdfjksdl@173"},function(data){ alert(data);}); </script>
<?php $str = "{$_GET["username"]}---{$_GET["email"]}---\n"; // $file = fopen("saveget.txt", "a"); // fwrite($file, $str); // $file.close(); echo $str; ?>
<?php //用来解决中文乱码问题 // header("Content-Type:text/html;charset=utf-8"); $str = "{$_POST["username"]}---{$_POST["email"]}---\n"; // $file = fopen("savepost.txt", "a"); // fwrite($file, $str); // $file.close(); //多个字符串会当成一个字符串来处理 // echo $str."我的啊"; // echo $str."******"; //所以可以写入一个数组中 // $arr = array("one"=>1,"two"=>2,"three"=>3); //转换成json // $json = json_encode($arr); // echo $json; echo $str; ?>