zoukankan      html  css  js  c++  java
  • 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:

  • 相关阅读:
    javascript 获取鼠标在盒子中的坐标
    jquery中clientY, pageY, screenY的区别,最后三张图一目了然
    javascript 小清新颜色翻页效果
    javascript 缓动返回顶部案例
    原生js轮播图实现
    javascript Math对象
    javascript 获取节点元素的封装
    javascript 转换大小写字母
    2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 1010
    2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 1009
  • 原文地址:https://www.cnblogs.com/cutepig/p/937429.html
Copyright © 2011-2022 走看看