Javascript 使用AJAX自动更新
我当前正在我的网页上使用此代码:Javascript 使用AJAX自动更新,javascript,php,jquery,ajax,setinterval,Javascript,Php,Jquery,Ajax,Setinterval,我当前正在我的网页上使用此代码: <?php $url = "https://www.toontownrewritten.com/api/invasions"; $data = json_decode(file_get_contents($url)); if (!empty($data->invasions)) { echo "<h1 style='text-align:center;margin:auto;padding:2px;font-size:16px;fon
<?php
$url = "https://www.toontownrewritten.com/api/invasions";
$data = json_decode(file_get_contents($url));
if (!empty($data->invasions)) {
echo "<h1 style='text-align:center;margin:auto;padding:2px;font-size:16px;font-weight:bold;text-decoration:underline;padding:2px;'>Invasion Tracker</h1>";
$i = 0;
foreach($data->invasions as $title => $inv) {
print "<h3 style='text-align:center;margin:auto;'><b>District:</b> {$title}
</h3><br style='font-size:1px;'><h3 style='text-align:center;margin:auto;'><b>Cog:</b> {$inv->type}
</h3><br style='font-size:1px;'><h3 style='text-align:center;margin:auto;'><b>Progress:</b> {$inv->progress}
</h3>";
if (count(($data->invasions) > 1)) {
if (end($data->invasions) !== $inv) {
print "<hr>";
} else {
print "<br style='font-size:2px;'>";
}
}
}
} else {
echo "<h1 style='text-align:center;margin:auto;padding:2px;color:darkred;font-weight:bold;'>No invasions!</span>";
}
?>
您的问题是未能理解AJAX。下面是一个$.post()
示例
首先,让我们创建希望客户端(浏览器用户)看到的页面:
viewsed.php
<?php
$out = '';
// you could even do your initial query here, but don't have to
?>
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8' />
<style type='text/css'>
@import 'whatever.css';
</style>
<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script>
<script type='text/javascript' src='whatever.js'></script>
</head>
<body>
<div id='output'><?php /* if initial query took place */ echo $out; ?></div>
</body>
</html>
在whatever.php
上:
<?php
// $assocArray = run database queries so you can create an Associative Array that can be converted to JSON
echo json_encode($assocArray);
?>
您的问题是未能理解AJAX。下面是一个$.post()
示例
首先,让我们创建希望客户端(浏览器用户)看到的页面:
viewsed.php
<?php
$out = '';
// you could even do your initial query here, but don't have to
?>
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8' />
<style type='text/css'>
@import 'whatever.css';
</style>
<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script>
<script type='text/javascript' src='whatever.js'></script>
</head>
<body>
<div id='output'><?php /* if initial query took place */ echo $out; ?></div>
</body>
</html>
在whatever.php
上:
<?php
// $assocArray = run database queries so you can create an Associative Array that can be converted to JSON
echo json_encode($assocArray);
?>
我有一个更好的建议,它同样与使用setInterval
相同
setInterval(function () {
if (isActive) return; // so that if any active ajax call is happening, don't go for one more ajax call
isActive = true;
try {
$.ajax("URL", params,function() { isActive = false;//successcallback }, function () {
isActive = false; // error callback
});
} catch (ex) { isActive = false;}
}, 10000);
我有一个更好的建议,它同样与使用setInterval
相同
setInterval(function () {
if (isActive) return; // so that if any active ajax call is happening, don't go for one more ajax call
isActive = true;
try {
$.ajax("URL", params,function() { isActive = false;//successcallback }, function () {
isActive = false; // error callback
});
} catch (ex) { isActive = false;}
}, 10000);
您只需使用建议的方法重新加载页面即可
但是如果你想有一个AJAX实现,它只是引用了html的一部分,那么你必须
差点忘了你的PHP代码
使用以下代码实现对url的请求
$.ajax({
url:“https://www.toontownrewritten.com/api/invasions",
})
.完成(功能(数据){
if(console&&console.log){
控制台日志(数据);
}
});代码>
制作一个JS
代码,将上一节中获得的数据
转换为可读的html,并显示在页面上。它应该在console.log(data)
所在的块中实现
把那部分代码放在一个setInterval中
setInterval(函数(){
//$.ajax();
}, 10000);代码>
要知道,如果你的请求不能在这段时间内完成,你将下地狱。看
您只需使用建议的方法重新加载页面即可
但是如果你想有一个AJAX实现,它只是引用了html的一部分,那么你必须
差点忘了你的PHP代码
使用以下代码实现对url的请求
$.ajax({
url:“https://www.toontownrewritten.com/api/invasions",
})
.完成(功能(数据){
if(console&&console.log){
控制台日志(数据);
}
});代码>
制作一个JS
代码,将上一节中获得的数据
转换为可读的html,并显示在页面上。它应该在console.log(data)
所在的块中实现
把那部分代码放在一个setInterval中
setInterval(函数(){
//$.ajax();
}, 10000);代码>
要知道,如果你的请求不能在这段时间内完成,你将下地狱。看
为什么不使用setinterval
?我试图使用它,但不知道如何使用它。我从来没有使用过AJAX,从来没有。如果我真的开始使用它,可能会让事情变得容易很多@洗发水Ajax不是武器,它只是一段代码,用户如何手动刷新页面?有链接吗?@shappo,setInterval
是邪恶的,使用setTimeout
@shappo是的,当前有一个链接供用户手动刷新页面。我宁愿让它自动刷新代码本身,而不是整个页面(一般来说,不要手动刷新)。我不知道我在用ajax做什么。为什么不使用setinterval
?我尝试过使用它,但不知道如何使用它。我从来没有使用过AJAX,从来没有。如果我真的开始使用它,可能会让事情变得容易很多@洗发水Ajax不是武器,它只是一段代码,用户如何手动刷新页面?有链接吗?@shappo,setInterval
是邪恶的,使用setTimeout
@shappo是的,当前有一个链接供用户手动刷新页面。我宁愿让它自动刷新代码本身,而不是整个页面(一般来说,不要手动刷新)。我不知道我在用ajax做什么。如果它的响应没有在10秒内出现,或者只是API需要更多的时间来评估,该怎么办?我该为/$.ajax()做些什么代码>?我是否把我包含的PHP代码放进去了?你的整个方法都错了。通常,在使用AJAX时,您会在单独的PHP页面上进行数据库查询,而不是在保存JavaScript的页面上进行查询。使用AJAX时所指的url
是PHP页面应该驻留的位置。使用PHP执行所有数据库查询,然后echo json\u encode($assocArray)代码>。这些JSON结果将在success
上发送回AJAX
。注意:有很多AJAX方法。@PHPglue我试过这个方法,但不起作用<代码>$(文档).ready(函数(){RefreshInvisions();});函数refreshInvisions(){$('#refreshinvs').load('ttr-invisions.php',函数(){setTimeout(refreshInvisions,10000);})}
我将php代码包装在
中。ttr-invasions.php是单独的文件。不,除非您不介意可能的闪存,否则您不想生成新的加载。我可能需要一秒钟才能给你一个更长的答案。等等。如果it响应不是在10秒内出现,或者只是API需要更多的时间来评估,该怎么办?我该为/$.ajax()做些什么代码>?我是否把我包含的PHP代码放进去了?你的整个方法都错了。通常,在使用AJAX时,您会在单独的PHP页面上进行数据库查询,而不是在保存JavaScript的页面上进行查询。使用AJAX时所指的url
是PHP页面应该驻留的位置。使用PHP执行所有数据库查询,然后echo json\u encode($assocArray)代码>。这些JSON结果将在success
上发送回AJAX
。注意:有很多AJAX方法。@PHPglue我试过这个方法,但不起作用<代码>$(文档).ready(函数(){RefreshInvisions();});函数refreshInvisions(){$('#refreshinvs').load('ttr-invisions)。