首页 > 代码库 > Ajax_2

Ajax_2

 

 

?
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
<script>
   <span style="background-color: rgb(192, 192, 192);"> var xmlHttp;</span>//创建一个对象
    function queryDetail(id){
        if(window.ActiveXObject){
            xmlHttp = new ActiveXObject("MICROSOFT.XMLHTTP");
        }else if(window.XMLHttpRequest){
            xmlHttp = new XMLHttpRequest();
        }
 
        if(!xmlHttp){
            alert("没有获取到对象!");
            return;
        }
 
<span style="background-color: rgb(0, 255, 0);">        //由于javascript是没有办法连接数据库的,所以,需要通过后台语言
        //结合来连接数据库,那么这里就是把js从界面获取的值,传递给后台程序
        //xmlHttp.open()其实也就是链接到一个后台程序,把值传递过去
        //这其实是第一步</span>
        xmlHttp<span style="background-color: rgb(255, 0, 0);">.open("GET","queryById.php?id="+id,true);</span>
 
<span style="background-color: rgb(0, 255, 0);">        //这是第三步
        //这一步是一个回调函数,回调函数其实就和打电话代办事情是一个意思
        //这个回调函数其实就是状态改变的时候,js应该做什么事情</span>
        xmlHttp<span style="background-color: rgb(255, 0, 0);">.onreadystatechange = function()</span>{
            if(xmlHttp.readyState == 4){
                if(xmlHttp.status == 200){
                    var txt = xmlHttp.responseText;
                    var show = document.getElementById("show")
                    if(show){
                        show.innerHTML = "";
                        document.body.removeChild(show);
                    }else{
                        var d = document.createElement("div");
                        d.id = "show";
                        d.style.cssText="width:200px;height:300px;border:1px solid red;";
                        var strs = txt.split(",");
                        alert(strs);
                        for(var i=0;i<strs.length;i++){
                            d.innerHTML += strs[i] + "<br/>";
                        }
 
 
                        document.body.appendChild(d);
                    }
 
 
                }
            }
        }
 
 
   <span style="background-color: rgb(0, 255, 0);">     //注意,open()方法就仅仅只是打开了连接,并没有传递,所以需要send方法
        //这是第二步</span>
        xmlHttp.<span style="background-color: rgb(255, 0, 0);">send(null);</span>
 
    }
 
 
 
 
</script>