Ajax的一个最简单例子.

{"Money":2000.00,"Age":21} 
main.htm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE> New Document </TITLE> 
<META NAME="Generator" CONTENT="EditPlus"> 
<META NAME="Author" CONTENT=""> 
<META NAME="Keywords" CONTENT=""> 
<META NAME="Description" CONTENT=""> 
<script type="text/javascript" src="shopajax.js"></script> 

</HEAD> 
<BODY> 
<div id="as"></div> 
<SCRIPT LANGUAGE="JavaScript"> 
<!-- 

var ajax=new xmlhttp(); 
debugger 
ajax.getopen(
"1.html"); 

function callpage()
    
if(ajax.status==0){//本地为0,远程为200 
    var obj=eval('('+ajax.text+')'); 
    document.getElementById(
"as").innerHTML="年纪:"+obj.Age+"<br>薪水:"+obj.Money; 
    }
 
}
 
//--> 
</SCRIPT> 
</BODY> 
</HTML> 


shopajax.js:

function requestHttp()
    
var request; 
    
if(window.XMLHttpRequest) 
        request 
= new XMLHttpRequest(); 
        
if(request.overrideMimeType) {request.overrideMimeType('text/xml'); 
        }
 
    }
 else if(window.ActiveXObject) 
        
var versions = ['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.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]);break;} 
        
catch(e) {} 
        }

    }
 
    
return request; 
}
 
function xmlhttp()
    
this.r=requestHttp(); 
}
 
xmlhttp.prototype.postopen
=function(url,data)
    
this.r.open('POST',url,false); 
    
this.r.setrequestheader("content-type","application/x-www-form-urlencoded"); 
    
this.r.onreadystatechange = ReadyStateChange(this); 
    
if(typeof(data)=='undefined') 
        
this.r.send(); 
    
else 
        
this.r.send(data); 
}
 
xmlhttp.prototype.getopen
=function(url)
    
if(window.XMLHttpRequest) {this.r.open('GET',url); 
    
this.r.onreadystatechange = ReadyStateChange(this); 
    
this.r.send(null); 
    }
 else 
        
this.r.open("GET", url, true); 
        
this.r.onreadystatechange = ReadyStateChange(this); 
        
this.r.send(); 
    }
 
}
 
ReadyStateChange
=function(obj)
    
return function()
        
if(obj.r.readyState==4)
            obj.status
=obj.r.status; 
            obj.text
=obj.r.responseText; 
            obj.body
=obj.r.responseBody; 
            callpage(); 
        }
 
    }
 
}
 


1.html:

Powered by Jekyll and Theme by solid

本站总访问量