A encapsulated Ajax class _ajax related to

Source: Internet
Author: User

Usage:
New Ajax (). Request (Url,cmd,async,method,poststring,title)
Parameters:
URL: Request page URL (required)
CMD: Return value processing function (required)
Async: Asynchronous, (Ture|false), default True
Method: Request mode, (Post|get), default get
Poststring: The request is in the form of post, the requested content
Title: Request Content Headers

Copy Code code as follows:

Ajax Encapsulation 2007-3-13
function Createxmlhttprequest () {
try {
if (window. XMLHttpRequest) {
return new XMLHttpRequest ();
}
else if (window. ActiveXObject) {
return new ActiveXObject ("Microsoft.XMLHTTP");
}
}
catch (E) {alert ("XMLHttpRequest object cannot be created!) Please check IE security settings! ");}
}
function Messagediv (t)
{
var v = document.createelement ("<div>");
v.innerhtml = "<table style=\" width:300px;\ "id=message>" +
"<tr style=\" Font-size:12px;background-color: #EEEEff; color: #227933; height:20px\ ">" +
"<td style=\" padding:2px;border-top:1px solid #E1E1E1 border-left:1px solid #E1E1E1 border-bottom:1px Solid 818181;border-right:1px solid #A1A1A1 \ ">" +
"<nobr> Connection not initialized ... </span></ Nobr></td></tr></table> ";
var L = document.getelementsbyname ("message"). Length;
V.style.csstext = "Position:absolute;bottom:" + (l*24) + "Px;left:0px;display:none";
Document.body.appendChild (v);
This.clear = function () {
Document.body.removeChild (v);
var msg = document.getelementsbyname ("message");
for (Var i=0;i<msg.length;i++) {
Msg[i].parentnode.style.csstext = "Position:absolute;bottom:" + (i*24) + "px;left:0px";
}
}
This.showmsg = function (s) {
V.style.display = "";
v.all.span1.innerhtml = s;
}
}
function Ajax () {
var x = new Createxmlhttprequest ();
This. Request = function (url,cmd,async,method,poststring,title) {
if (method!= "post") method = "POST"; else method = ' get ';
if (async!=true) async = true; else Async = false;
if (typeof (Poststring)!= "string") poststring= "";
if (typeof (title)!= "string") title= "fetching data"; Else title= "is getting" + title;
var MsgBox = new Messagediv (title);
X.onreadystatechange = function ()
{
if (async) switch (x.readystate) {
Case 1:
Msgbox.showmsg ("Initializing connection ...");
Return
Case 2:
Msgbox.showmsg ("Sending data ...");
Return
Case 3:
Msgbox.showmsg ("receiving data ...");
Return
Case 4:
Msgbox.showmsg ("Data reception complete ...");
if (X.status = = 200) {
CMD (x.responsetext);
Msgbox.clear ();
}
else {
Msgbox.showmsg ("Request failed," + X.statustext + "(" + X.status +) ");
SetTimeout (msgbox.clear,3000);
}
Return
}
}
X.open (Method,url,async);
if (method== "post") {msgbox.showmsg ("receiving data ..."); X.send (poststring);} else X.send ();
if (!async) {
Msgbox.showmsg ("Data reception complete ...");
CMD (x.responsetext);
Msgbox.clear ();
}
}
}

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.