2011-07-18 130 views
0

我试图制作一个HTML页面,在警报中显示另一个html文件;但按下触发按钮时不会显示。使用AJAX显示HTML弹出窗口

<html> 
    <head> 
     <script> 

     var xmlhttp=new XMLHttpRequest(); 

     function pop() { 
      xmlhttp.open("GET","content.html",true); 
      xmlhttp.send(); 
      xmlhttp.onreadystatechange=function() { 
       if(xmlhttp.readystate==4&&xmlhttp.status==200) { 
        alert(xmlhttp.responseText); 
       } 
      } 
     } 

     </script> 
    </head> 
    <body> 
     <input type="button" name="test" value="push" onclick="pop()"> 
    </body> 
</html> 

这里是content.html

<html> 
    <body> 
     Hi! 
    </body> 
</html> 
+1

您可能想要使用jQuery。为您节省一些工作,您的代码将更好地阅读。 http://api.jquery.com/category/ajax/ – ThiefMaster

+1

这听起来非常像我的功课。 –

+0

我怎么能obtian嗨!而不是整个html代码???谢谢 –

回答

1

实际上它是readyState。 JavaScript区分大小写。

此外,设置所有内容后发送可能会更好。

最后,你有一个失踪}

var xmlhttp=new XMLHttpRequest(); 

function pop() 
{ 
xmlhttp.open("GET","content.html",true); 
xmlhttp.onreadystatechange=function() 
{ if(xmlhttp.readyState==4&&xmlhttp.status==200) 
    {alert(xmlhttp.responseText);} 
} 
xmlhttp.send(); 
} 
+0

好的。感谢大家。看来我犯了一些粗心的错误。 –

0

没错的内容,我就指望3个开括号({),但只有两个花括号(})。检查浏览器的错误控制台以发现此类错误。

+0

我怎么可以obtian嗨!而不是整个html代码???谢谢 –

0

检查如下:缺少关闭括号。

var xmlhttp = new XMLHttpRequest();

function pop() 
{ 
    xmlhttp.open("GET","content.html",true); 
    xmlhttp.send(); 
    xmlhttp.onreadystatechange=function() 
    { 

if(xmlhttp.readystate==4&&xmlhttp.status==200) 
    { 
    alert(xmlhttp.responseText); 
    } 
    } 
}