2012-03-25 107 views
2

任何幫助在這裏都是值得讚賞的。我正在php中構建一個web應用程序,並且使用了有很多內置工具的Yii MVC框架。正如標題所說,我需要每隔10秒刷新一次div。目前,我有這個AJAX功能如何使用jquery或ajax以10秒爲間隔刷新div

<script type="text/javascript"> 
    function ajaxFunction(){ 
    var ajaxRequest; 

    try{ 
     // Opera 8.0+, Firefox, Safari 
     ajaxRequest = new XMLHttpRequest(); 
    } catch (e){ 
     // Internet Explorer Browsers 
     try{ 
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
      try{ 
       ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
      } catch (e){ 
       // Something went wrong 
       alert("Your browser broke!"); 
       return false; 
      } 
     } 
    } 
    // Create a function that will receive data sent from the server 
    ajaxRequest.onreadystatechange = function(){ 
       var list = document.getElementById('logged_in_users_list'); 
     if(ajaxRequest.readyState == 4){ 
      list.innerHTML = ajaxRequest.responseText; 
         setTimeout('ajaxFunction()',10000); 
     } 
    } 
    ajaxRequest.open("GET", "protected/controllers/room/openRoom", true); 
    ajaxRequest.send(null); 
} 
</script> 


<script type="text/javascript"> 
      setInterval(function() {ajaxFunction();}, 5000); 
</script> 

對於那些你們誰不熟悉的Yii它存儲大多數你的PHP文件名爲保護的文件夾中。那麼它就是這樣,上面的代碼的ajaxRequest.open行請求該URL存儲在受保護的文件夾內,所以我一直得到訪問禁止403錯誤。任何想法如何我可以實現與jQuery不同的東西或繞過此訪問問題?

回答

5

頁使用jQuery的

$(function() { 
    function callAjax(){ 
     $('#myDiv').load("http://yourdomain.com"); 
    } 
    setInterval(callAjax, 5000); 
}); 
0

粗略等同於jQuery的代碼是這樣的:

//execute call immediately 
(function check(){ 
    //a GET AJAX call 
    $.get('protected/controllers/room/openRoom') 
    .done(function(data){ 
     //when we receive, populate 
     $('#logged_in_users_list').html(data); 
    }) 
    .always(function(){ 
     //regardless of a fail or success, call again after 10 seconds 
     setTimeout(check,10000); 
    }); 
}()); 

和403將永遠是403就是這樣告訴你,你是不是允許進入該位置的代碼(也許你需要驗證? )

0
// zisu.php 

    <html> 
    <head> 
    <script type="text/javascript"> 
    var auto_refresh = setInterval(
      function() 
      { 
      $('#div1').load('time.php'); 
      }, 10000); 
    </script> 
    </head> 
    <body> 
    <div id ="div1"> 
    <?php 
    echo date("h:i:s A"); 
    ?> 
    </div> 
    </body> 
    </html> 


// time.php 
    <?php 
    echo date("h:i:s A"); 
    ?>