2012-03-25 106 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"); 
    ?>