导读:本文实例讲述了一个简单Ajax类库及使用方法。分享给大家供大家参考,具体如下:ajax.js12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576functionAjax(recvType){&nb
文中案例叙述了一个简易Ajax类库及操作方法。共享给大伙儿供各位参照,实际方式:
ajax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
function Ajax(recvType){
var aj=new Object();
aj.recvType=recvType ? recvType.toUpperCase() : 'HTML' //HTML XML
aj.targetUrl='';
aj.sendString='';
aj.resultHandle=null;
aj.createXMLHttpRequest=function(){
var request=false;
//window目标中有XMLHttpRequest存有就是是非非IE,包含(IE7,IE8)
if(window.XMLHttpRequest){
request=new XMLHttpRequest();
if(request.overrideMimeType){
request.overrideMimeType("text/xml");
}
//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'];
for(var i=0; i<versions.length; i ){
try{
request=new ActiveXObject(versions[i]);
if(request){
return request;
}
}catch(e){
request=false;
}
}
}
return request;
}
aj.XMLHttpRequest=aj.createXMLHttpRequest();
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);
}
}
}
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();
}
}
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 aj;
}
|
操作方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<script type="text/javascript">
var ajax = Ajax(); // 创建对象目标,默认设置为 HTTP,假如传到 XML,回到 XML 目标
// Ajax 的 get 方式 使用说明书
/**
* function(targetUrl, resultHandle)
* @param string targetUrl 传送以往的 URL 详细地址
* @param string resultHandle 调用函数,选择项
*/
ajax.get('test.php?name=liruxing&email=liruxing1715@sina.com', function(data) {
eval("var obj=" data);
alert(obj.name);
alert(obj.email);
});
// Ajax 的 post 方式 使用说明书
/**
* function(targetUrl, sendString, resultHandle)
* @param string targetUrl 传送以往的 URL 详细地址
* @param string sendString 变量值
* @param string resultHandle 调用函数,选择项
*/
ajax.post('test.php', 'name=liruxing&email=liruxing1715@sina.com', function(data){
eval("var obj=" data);
alert(obj.name);
alert(obj.email);
})
// post 第二种文件格式,第二个主要参数为 Json 文件格式
ajax.post('test.php', {name:'李茹星',email:'liruxing1715@sina.com'}, function(data){
eval("var obj=" data);
alert(obj.name);
alert(obj.email);
})
</script>
|
标签:Ajax类库