【问题标题】:Using Ajax to change the inner content of a div使用 Ajax 更改 div 的内部内容
【发布时间】:2012-11-29 13:50:42
【问题描述】:

我是 Ajax 的初学者,我有这段 html 代码,旨在通过使用 xmlhttprequest 请求不同的 html 地址并将其内容放入 div 中来更改 div 的内部内容。我究竟做错了什么?代码如下:

    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <script type="text/javascript">
        var xmlhttp= null;
        if(window.XMLHttpRequest){
            xmlhttp = new XMLHttpRequest();
        }else{
            alert("You must be using a different browser.");
        }

        function makeRequest(serverPage,objID){

            var obj = document.getElementById(objID);
            xmlhttp.open("GET",serverPage);
            xmlhttp.onreadystatechange = function(){
                if(xmlhttp.readyState == 4 && xmlhttp.status == 100){
                    obj.innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.send(null);
        }
    </script>
</head>
<body onload="makeRequest ('content1.html','hw')">
    <div align="center">
        <h1>My Webpage</h1>
        <a href="content1.html" onclick="makeRequest('content1.html','hw'); return false;"> Page1</a> | <a href="content2.html" onclick="makeRequest('content2.html','hw'); return false;"> Page2</a> | <a href="content3.html" onclick="makeRequest('content3.html','hw'); return false;"> Page3</a> | <a href="content4.html" onclick="makeRequest(content4.html,hw); return false;"> Page4</a>
        <div id = "hw"></div>
</body>

【问题讨论】:

    标签: javascript html ajax xmlhttprequest server-side


    【解决方案1】:

    一般来说,这对我来说看起来不错。

    不过,xmlhttp.status == 100 检查看起来很可疑。

    100 是一个不寻常的 HTTP 状态代码。通常,Web 服务器会在请求成功时返回 200(“OK”)。

    尝试将status == 100 检查替换为status == 200

    参考请见:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

    【讨论】:

      【解决方案2】:

      xmlhttp.status == 100 更改为 xmlhttp.status == 200 检查是否可以解决您的问题

      如果您尝试在 IE 中运行该页面,请尝试添加此行

      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      

      【讨论】:

        【解决方案3】:

        测试 xmlhttp.status == 200 而不是 100。

        http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

        【讨论】:

          【解决方案4】:

          我猜应该是:

          function makeRequest(serverPage,objID){
              xmlhttp.open("GET",serverPage);
              xmlhttp.onreadystatechange = function(){
                  if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
                      document.getElementById(objID).innerHTML = xmlhttp.responseText;
                  }
              }
              xmlhttp.send(null);
          }
          

          【讨论】:

            【解决方案5】:
            function AjaxGet(url,helm)
            { 
              if (xmlhttp == null)
              {  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)
                 {  if ( (xmlhttp.status==200) ||  (xmlhttp.status==304) )
                      { document.getElementById(helm).innerHTML = xmlhttp.responseText; }
                      else { document.getElementById(helm).innerHTML = xmlhttp.status + " " +xmlhttp.statusText; }
                }
             }
            
               xmlhttp.open("GET",url,true);
               xmlhttp.send();
            }
            

            【讨论】:

              猜你喜欢
              • 2013-09-01
              • 2011-09-24
              • 2012-10-20
              • 1970-01-01
              • 2021-02-11
              • 2021-11-14
              • 2012-06-03
              • 2013-06-26
              • 1970-01-01
              相关资源
              最近更新 更多