任何帮助都在这里受到赞赏。我正在用PHP构建一个Web应用程序,我正在使用Yii MVC框架,它有很多内置工具。正如标题所说,我需要每隔10秒刷新一次div。目前我有这个ajax功能
<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文件存储在名为protected的文件夹中。好吧就是这样,上面的ajaxRequest.open代码行请求存储在受保护文件夹中的url,所以我不断获取访问禁止403错误。任何想法如何实现与jquery不同的东西或解决这个访问问题?
答案 0 :(得分:5)
使用jquery
$(function() {
function callAjax(){
$('#myDiv').load("http://yourdomain.com");
}
setInterval(callAjax, 5000 );
});
答案 1 :(得分:1)
与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.这是代码,告诉您不允许您进入该位置(可能需要身份验证?)
答案 2 :(得分: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");
?>