Useful Ajax class code sharing _ajax related

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.