1.JS文件
1 /*
2 作者:herobeast|王永强
3 时间:2007-10-9
4 调用:
5 var ajax = new Ajax("xmlhttp.aspx","");
6 var retvalue = ajax.sendXmlHttp();
7 function callBackHanlder(status);//回调方法
8 */
9 var objxml = null;
10 function Ajax(strUrl,xmlContent)
11 {
12 this.url = strUrl; //url
13 this.xmlcontent = xmlContent;//xml document
14 this.xmlhttp = this.createXmlHttp(); // create new object
15
16 }
17
18 //create a new object
19 Ajax.prototype.createXmlHttp = function()
20 {
21 var xmlhttpobj = false;
22 if(window.ActiveXObject)
23 {
24 //IE(此处的IE泛指所有用IE内核的浏览器,下同)
25 xmlhttpobj=new ActiveXObject("Msxml2.XMLHTTP");
26 if(!xmlhttpobj)
27 {
28 xmlhttpobj=new ActiveXObject("Microsoft.XMLHTTP")
29 }
30 }
31 else if(window.XMLHttpRequest)
32 {
33 //Mozilla(FireFox,NS)
34 xmlhttpobj=new XMLHttpRequest()
35 }
36 return xmlhttpobj;
37 }
38 //同步
39 Ajax.prototype.sendXmlHttp= function()
40 {
41 //第一个参数的意思是,用"POST"方式发送数据.可以大到4MB,也可以换为"GET".只能256KB
42 //第2个参数的意思是数据发送到哪个文件处理
43 //第3个参数意思是同步或异步方式.TRUE为异步,FALSE为同步
44 objxml = this.xmlhttp;
45 this.xmlhttp.onreadystatechange = function(){Ajax.handleStateChange(objxml)};
46
47 this.xmlhttp.open("POST", this.url, false);
48 this.xmlhttp.setRequestHeader("Content-Type","text/xml");
49
50 if(this.xmlcontent.length==0)
51 {
52 this.xmlhttp.send("<?xml version=\"1.0\" encoding=\"utf-8\"?><root></root>");
53 }
54 else
55 {
56
57 this.xmlhttp.send(this.xmlcontent);
58 }
59
60 var retvalue = "";
61 if(this.xmlhttp.status !=200)
62 {
63 alert(this.xmlhttp.statusText);
64 }
65 else
66 {
67 retvalue = this.xmlhttp.responseText;
68 }
69 objxml = null;
70 //返回结果
71 return retvalue;
72 }
73
74 Ajax.handleStateChange = function (objxmlhttp)
75 {
76 var xmlhttpstatus = "";
77 //收到完整的服务器响应
78 if(objxmlhttp.readyState == 1)
79 {
80 xmlhttpstatus = "连接服务器";
81
82 }
83 else if(objxmlhttp.readyState == 2)
84 {
85 xmlhttpstatus = "开始加载数据";
86
87 }
88 else if(objxmlhttp.readyState == 3)
89 {
90 xmlhttpstatus = "正在加载数据";
91
92 }
93 else if(objxmlhttp.readyState == 4)
94 {
95 //HTTP服务器响应值成功
96 if(objxmlhttp.status == 200)
97 {
98 //将服务器返回的字符串写到页面中ID为showdiv的区域
99 xmlhttpstatus = "加载完成";
100
101 }
102
103 }
104 else
105 {
106 xmlhttpstatus = "错误"+objxmlhttp.statusText+"请重新选择";
107 x
108 }
109 callBackHanlder(xmlhttpstatus);
110 }
111
112
2 作者:herobeast|王永强
3 时间:2007-10-9
4 调用:
5 var ajax = new Ajax("xmlhttp.aspx","");
6 var retvalue = ajax.sendXmlHttp();
7 function callBackHanlder(status);//回调方法
8 */
9 var objxml = null;
10 function Ajax(strUrl,xmlContent)
11 {
12 this.url = strUrl; //url
13 this.xmlcontent = xmlContent;//xml document
14 this.xmlhttp = this.createXmlHttp(); // create new object
15
16 }
17
18 //create a new object
19 Ajax.prototype.createXmlHttp = function()
20 {
21 var xmlhttpobj = false;
22 if(window.ActiveXObject)
23 {
24 //IE(此处的IE泛指所有用IE内核的浏览器,下同)
25 xmlhttpobj=new ActiveXObject("Msxml2.XMLHTTP");
26 if(!xmlhttpobj)
27 {
28 xmlhttpobj=new ActiveXObject("Microsoft.XMLHTTP")
29 }
30 }
31 else if(window.XMLHttpRequest)
32 {
33 //Mozilla(FireFox,NS)
34 xmlhttpobj=new XMLHttpRequest()
35 }
36 return xmlhttpobj;
37 }
38 //同步
39 Ajax.prototype.sendXmlHttp= function()
40 {
41 //第一个参数的意思是,用"POST"方式发送数据.可以大到4MB,也可以换为"GET".只能256KB
42 //第2个参数的意思是数据发送到哪个文件处理
43 //第3个参数意思是同步或异步方式.TRUE为异步,FALSE为同步
44 objxml = this.xmlhttp;
45 this.xmlhttp.onreadystatechange = function(){Ajax.handleStateChange(objxml)};
46
47 this.xmlhttp.open("POST", this.url, false);
48 this.xmlhttp.setRequestHeader("Content-Type","text/xml");
49
50 if(this.xmlcontent.length==0)
51 {
52 this.xmlhttp.send("<?xml version=\"1.0\" encoding=\"utf-8\"?><root></root>");
53 }
54 else
55 {
56
57 this.xmlhttp.send(this.xmlcontent);
58 }
59
60 var retvalue = "";
61 if(this.xmlhttp.status !=200)
62 {
63 alert(this.xmlhttp.statusText);
64 }
65 else
66 {
67 retvalue = this.xmlhttp.responseText;
68 }
69 objxml = null;
70 //返回结果
71 return retvalue;
72 }
73
74 Ajax.handleStateChange = function (objxmlhttp)
75 {
76 var xmlhttpstatus = "";
77 //收到完整的服务器响应
78 if(objxmlhttp.readyState == 1)
79 {
80 xmlhttpstatus = "连接服务器";
81
82 }
83 else if(objxmlhttp.readyState == 2)
84 {
85 xmlhttpstatus = "开始加载数据";
86
87 }
88 else if(objxmlhttp.readyState == 3)
89 {
90 xmlhttpstatus = "正在加载数据";
91
92 }
93 else if(objxmlhttp.readyState == 4)
94 {
95 //HTTP服务器响应值成功
96 if(objxmlhttp.status == 200)
97 {
98 //将服务器返回的字符串写到页面中ID为showdiv的区域
99 xmlhttpstatus = "加载完成";
100
101 }
102
103 }
104 else
105 {
106 xmlhttpstatus = "错误"+objxmlhttp.statusText+"请重新选择";
107 x
108 }
109 callBackHanlder(xmlhttpstatus);
110 }
111
112
2.使用方法
1 <script src="Ajax.js" type="text/jscript"></script>
2 <script type="text/javascript">
3 function TestAjax()
4 {
5
6 var url = "xmlhttp.aspx?ID=100;
7 var ajax = new Ajax(url,"");
8
9 var ret = ajax.sendXmlHttp();
10 }
11 function callBackHanlder(status)
12 {
13
14 }
15 </script>
2 <script type="text/javascript">
3 function TestAjax()
4 {
5
6 var url = "xmlhttp.aspx?ID=100;
7 var ajax = new Ajax(url,"");
8
9 var ret = ajax.sendXmlHttp();
10 }
11 function callBackHanlder(status)
12 {
13
14 }
15 </script>
3.后台调用方法
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 //url参数
4 string id = "";
5 if(Request.Qustring["id"]!=null)
6 {
7 id = Request.Qustring["id"].ToString();
8 }
9 //xml参数
10 XmlDocument xmldoc = new XmlDocument();
11 xmldoc.Load(Request.InputStream);
12 string xmlcontent = xmldoc.InnerXml;
13 Operate(id,xmlcontent);
14 }
15 public void Operate(string id,string xmlcontent)
16 {
//返回值
17 Response.Write("ID="+id+"xml="+xmlcontent);
18 }
2 {
3 //url参数
4 string id = "";
5 if(Request.Qustring["id"]!=null)
6 {
7 id = Request.Qustring["id"].ToString();
8 }
9 //xml参数
10 XmlDocument xmldoc = new XmlDocument();
11 xmldoc.Load(Request.InputStream);
12 string xmlcontent = xmldoc.InnerXml;
13 Operate(id,xmlcontent);
14 }
15 public void Operate(string id,string xmlcontent)
16 {
//返回值
17 Response.Write("ID="+id+"xml="+xmlcontent);
18 }
源文件:Ajax.rar