码迷,mamicode.com
首页 > 其他好文 > 详细

Ajax_2

时间:2014-05-09 08:00:14      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   ext   

 

 

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>

  

Ajax_2,布布扣,bubuko.com

Ajax_2

标签:style   blog   class   code   java   ext   

原文地址:http://www.cnblogs.com/5huihui/p/3717879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!