| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- /**
- *Ajax简易框架
- */
- function Ajax(url,callback,method)
- {
- this.url=url;
- this.callback=callback;
- this.method=method==null?"GET":method;
- this.xmlhttp=this.GetXMLHttpRequest();
- }
- /**
- *生成成成xmlhttpRequest对象
- */
- Ajax.prototype.GetXMLHttpRequest=function (){
- var xmlhttp=false;
- if(window.ActiveXObject){//IE浏览器
- try{
- xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
- }catch(e){
- try{
- xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
- }catch(e){
- xmlhttp=false;
- }
- }
- }else if(window.XMLHttpRequest){//支持支持支持Mozilla、FireFox、Opera等浏览器
- try{
- xmlhttp=new XMLHttpRequest();
- }catch(e){
- xmlhttp=false;
- }
- }
- if(!xmlhttp){
- alert("无法创建XMLHttpRequest对象");
- }
- return xmlhttp;
- }
- /**
- *创建请求并发送
- */
- Ajax.prototype.send=function (){
- if(this.xmlhttp){
- oThis=this;
- this.xmlhttp.open(this.method,this.url,true);
- this.xmlhttp.onreadystatechange=function(){
- oThis.processResult();
- };
- this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');//如果method是post时
- this.xmlhttp.send(null);
- }
- }
- /**
- *接收请求数据
- */
- Ajax.prototype.processResult=function (){
- var athis=this;
- if(this.xmlhttp.readystate==null){
- this.xmlhttp.addEventListener("load",function(){
- athis.result();
- },false);
- this.xmlhttp.readystate=1;
- }
- if(this.xmlhttp.readystate==4){
- this.result();
- }
- }
- /**
- *得到结果
- */
- Ajax.prototype.result=function(){
- if(this.xmlhttp.status==200&&this.xmlhttp.statusText=="OK"){
- this.callback(this.xmlhttp);
- }else {
- alert("数据请求失败");
- }
- }
|