首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

simpleAjax

2012-03-02 
simpleAjax - Web 开发 / Ajax希望点击按钮后,弹出有XML文件中文本信息的对话框:HTML codehtmlheadti

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中的么?
------解决方案--------------------


用浏览器来查看

用编辑器的插件有可能未完全实现浏览器的功能。

而且如果请求的是xml文件,要通过服务器来访问,要不在ie下生成不了xml的dom树结构

其他问题参考
ajax问题总结

热点排行