Ajax.js
-------------------------[Ajax classes]--------------------------
Copy Code code as follows:
function Ajax (recvtype) {
var aj=new Object ();
Aj.recvtype=recvtype? Recvtype.touppercase (): ' HTML '; The type of file passed to the formal parameter
Aj.targeturl= ';
Aj.sendstring= ';
Aj.resulthandle=null;
/* Create XMLHttpRequest Object * *
Aj.createxmlhttprequest=function () {
var xmlHttp = false;
if (window. XMLHttpRequest) {//Create XMLHttpRequest object in non IE
XmlHttp = new XMLHttpRequest ();
}else if (window. ActiveXObject) {
try{
XmlHttp = new ActiveXObject ("Msxml2.xmlhttp"); Create by new IE
}catch (Error1) {//Create failed
try{
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP"); By the old version of IE created
}catch (ERROR2) {//Create failed
XmlHttp = false;
}
}
}
return xmlHttp;
}
Aj. Xmlhttprequest=aj.createxmlhttprequest ();
/* Processing Server response * *
Aj.processhandle=function () {
if (AJ. Xmlhttprequest.readystate = = 4) {
if (AJ. Xmlhttprequest.status = = 200) {
if (aj.recvtype== "HTML")
Aj.resulthandle (AJ. Xmlhttprequest.responsetext);
else if (aj.recvtype== "XML")
Aj.resulthandle (AJ. Xmlhttprequest.responsexml);
}
}
}
/* Defines a method to be passed using the Get method * *
Aj.get=function (TargetUrl, Resulthandle) {
Aj.targeturl=targeturl;
if (resulthandle!=null) {
Aj. Xmlhttprequest.onreadystatechange=aj.processhandle;
Aj.resulthandle=resulthandle;
}
if (window. XMLHttpRequest) {
Aj. Xmlhttprequest.open ("Get", aj.targeturl);
Aj. Xmlhttprequest.send (NULL);
}else{
Aj. Xmlhttprequest.open ("Get", Aj.targeturl, True);
Aj. Xmlhttprequest.send ();
}
}
/* Defines a method to be passed using the POST method * *
Aj.post=function (TargetUrl, sendstring, Resulthandle) {
Aj.targeturl=targeturl;
if (typeof (sendstring) = = "Object") {
var str= "";
For (Var pro in sendstring) {
str+=pro+ "=" +sendstring[pro]+ "&";
}
Aj.sendstring=str.substr (0, str.length-1);
}else{
aj.sendstring=sendstring;
}
if (resulthandle!=null) {
Aj. Xmlhttprequest.onreadystatechange=aj.processhandle;
Aj.resulthandle=resulthandle;
}
Aj. Xmlhttprequest.open ("Post", TargetUrl);
Aj. Xmlhttprequest.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Aj. Xmlhttprequest.send (aj.sendstring);
}
return to AJ;
}
-------------------------[use Method]--------------------------
Useajax.html
Copy Code code as follows:
<script src= "Ajax.js" ></script>
<script language= "javascript" type= "Text/javascript" >
var ajax=ajax ();
/*get Use mode * *
Ajax.get ("server.php?name=zhangsan&phone=778", function (data) {
alert (data); Data is read from the server side
});
/* The first post use mode * *
/* Ajax.post ("server.php", "name=ligang&phone=222", function (data) {
alert (data);
});
*/
/* The second post use mode * *
/* Ajax.post ("server.php", {name: "Tom", Phone: "456"},function (data) {
alert (data);
});
*/
</script>
server.php
<?php
Header ("content-type:text/html;charset=gb2312");
$str = "Name: {$_get[" name "]}\n Tel: {$_get[" phone "]}";
Echo $str;
?>
Enter the address of the useajax.html in the browser, if it appears
The Ajax method uses the correct