ajax用onclick显示2个数据请求

时间:2012-04-05 08:29:49

标签: javascript ajax

你好我仍然是ajax的新手,我想在不同的地方显示2个数据。

这里是代码

 <li onclick="showPost(this.value);" value="*digit*" >lala</li>

javascript

<script>
if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        function showPost(hal)
        {
            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("gallekanan").innerHTML=xmlhttp.responseText;  
                }
            }       
            xmlhttp.open("GET","../wp-content/themes/koolfort/example.php?pal="+hal,true);
            xmlhttp.send();
            showJudul(hal);
        }

        function showJudul(hal)
        {
            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("eventjudul").innerHTML=xmlhttp.responseText;  
                }
            }       
            xmlhttp.open("GET","../wp-content/themes/koolfort/example1.php?pal="+hal,true);
            xmlhttp.send();
        }
</script>

当我运行代码时,只运行showJudul并且showPost被中止。

2 个答案:

答案 0 :(得分:0)

移动showJudul(hal);紧跟在document.getElementById(“gallekanan”)之后.innerHTML = xmlhttp.responseText;

答案 1 :(得分:0)

尝试对每个函数进行单独控制,而不是在另一个函数中调用它们。即..将其作为

<li onclick="showPost(this.value); showJudul(this.value);" value="*digit*" >lala</li>

<script>

        function showPost(hal)
        {
if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("gallekanan").innerHTML=xmlhttp.responseText;  
                }
            }       
            xmlhttp.open("GET","../wp-content/themes/koolfort/example.php?pal="+hal,true);
            xmlhttp.send();

        }

        function showJudul(hal)
        {
if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("eventjudul").innerHTML=xmlhttp.responseText;  
                }
            }       
            xmlhttp.open("GET","../wp-content/themes/koolfort/example1.php?pal="+hal,true);
            xmlhttp.send();
        }
</script>

还尝试使用try {} catch {},在那里尝试初始化xmlhttp - 如果没有这样的对象,函数可能会出错...