当前位置: 代码迷 >> Ajax >> simpleAjax
  详细解决方案

simpleAjax

热度:322   发布时间:2012-03-02 14:40:28.0
simpleAjax - Web 开发 / Ajax
希望点击按钮后,弹出有XML文件中文本信息的对话框:
HTML code

<html>
    <head>
    <title>simpleAjax</title>
          <script type="text/javascript">
        var xmlHttpRequest;
        function createXMLHttpRequest() 
        {
            if (typeof XMLHttpRequest=="undefined")  
            {
                    xmlHttpRequest=new ActiveXObject(navigator.userAgent.indexOf("MSIE 5")>=0?"Microsoft.XMLHTTP":"Msxml2.XMLHTTP");
            }
            else
            {
                    xmlHttpRequest= new XMLHttpRequest();                      
            }
        } 
        
        function sendRequest()
        {
            createXMLHttpRequest();
            xmlHttpRequest.onreadystatechange=stateChange;
            xmlHttpRequest.open("GET","test.xml");
            xmlHttpRequest.send(null);    
        }
        
        function stateChange()
        {
            if(xmlHttpRequest.readyState==4)
            {
                if(xmlHttpRequest==200)
                {
                    window.alert(xmlHttpRequest.responseText);
                }                
            }            
        }
        </script>
    </head>
  
  <body> 
    <form action="#">
        <input type="button" value="send" onclick="sendRequest();"/>
    </form>
  </body>
</html>



这是在书上抄的例子,怎么点send按钮没反应呢,JS代码里有错吗?我用MyEclipse,装了个JSEclipse插件,提示有个警告,说:
Declaration of variable ActiveXObject was not found in function createXMLHttpRequest or enclosing scope
Declaration of variable XMLHttpRequest was not found in function createXMLHttpRequest or enclosing scope
ActiveXObject和 XMLHttpRequest不是浏览器的两个对象吗,之前的IE版本把XMLHttpRequest对象当成ActiveXObject其他浏览器就是XMLHttpRequest

请大家来帮忙啊

------解决方案--------------------
HTML code
<html>
    <head>
    <title>simpleAjax</title>
          <script type="text/javascript">
              var xmlHttpRequest;
              function createXMLHttpRequest() {
                  if (typeof XMLHttpRequest == "undefined") {
                      xmlHttpRequest = new ActiveXObject(navigator.userAgent.indexOf("MSIE 5") >= 0 ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP");
                  }
                  else {
                      xmlHttpRequest = new XMLHttpRequest();
                  }
              }

              function sendRequest() {
                  createXMLHttpRequest();
                  //alert("df");
                  xmlHttpRequest.onreadystatechange = stateChange;
                  xmlHttpRequest.open("GET", "test.xml");
                  xmlHttpRequest.send(null);
              }

              function stateChange() {
                  if (xmlHttpRequest.readyState == 4) {
                  
                      if (xmlHttpRequest.status  == 200) {
                          alert("done");
                          alert(this.responseXML);
                      }
                  }
              }
        </script>
    </head>
  
  <body> 
    <form action="#">
        <input type="button" value="send" onclick="sendRequest();"/>
    </form>
  </body>
</html>

------解决方案--------------------
不可能啊,在我的电脑上能执行的啊,而且xmlHttpRequest.status == 200而不是xmlHttpRequest == 200
------解决方案--------------------
你这些代码是放在jsp中的么?
------解决方案--------------------
  相关解决方案