作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个页面每 4 秒使用 Ajax 调用一个 PHP 脚本。 PHP 脚本会输出一些文本,然后将这些文本放在页面上的一个 div 中。 (PHP 脚本确实连接到 MySQL 服务器,这可能是我出现以下问题的原因吗?)
这是奇怪的部分。第一次调用 PHP 脚本时,它会很好地回显所有文本。然而,4 秒后,当 Ajax 再次调用 PHP 脚本时,页面变为空白。更奇怪的是,当我在 MS Edge 上右键单击查看源代码时,所有页面内容都会加载,然后如果我关闭源代码,它会在 4 秒后再次消失。
有人知道为什么会这样吗?
我的带有 Ajax 的 JS 文件:
function updateCandles(){
var candleIndexStart = $('#candle-index-start').text();
var candleSort = $('#candle-sort').text();
var resultArray = $.ajax({
type: 'GET',
url: 'php/display_candles.php',
data: {
'sort': candleSort,
'candle-start': candleIndexStart
},
async: false
}).responseText.split("h8ktncuidabciafvabc7932hcueqbc73gd8713gdy713gd6831df8136fd13d");
$('.candles').html(resultArray[0]);
$('#candle-counter').html(resultArray[1]);
setTimeout(updateCandles, 4000);
}
$(document).ready(function(){
updateCandles();
});
我的 PHP 脚本:
<?php
session_start();
function timeAgo($time_ago)
{
$time_ago = strtotime($time_ago);
$cur_time = time();
$time_elapsed = $cur_time - $time_ago;
$seconds = $time_elapsed ;
$minutes = round($time_elapsed / 60 );
$hours = round($time_elapsed / 3600);
$days = round($time_elapsed / 86400 );
$weeks = round($time_elapsed / 604800);
$months = round($time_elapsed / 2600640 );
$years = round($time_elapsed / 31207680 );
// Seconds
if($seconds <= 60){
return "just now";
}
//Minutes
else if($minutes <=60){
if($minutes==1){
return "one minute ago";
}
else{
return "$minutes minutes ago";
}
}
//Hours
else if($hours <=24){
if($hours==1){
return "an hour ago";
}else{
return "$hours hrs ago";
}
}
//Days
else if($days <= 7){
if($days==1){
return "yesterday";
}else{
return "$days days ago";
}
}
//Weeks
else if($weeks <= 4.3){
if($weeks==1){
return "a week ago";
}else{
return "$weeks weeks ago";
}
}
//Months
else if($months <=12){
if($months==1){
return "a month ago";
}else{
return "$months months ago";
}
}
//Years
else{
if($years==1){
return "one year ago";
}else{
return "$years years ago";
}
}
}
$connection = mysqli_connect("localhost", "username", "pass", "db");
if(!$connection) {
echo "Error";
}
if($_GET['teacher'] == 'yes'){
$result2 = mysqli_query($connection, "SELECT * FROM candles WHERE approved=0 ORDER BY date desc LIMIT " . $_GET['candle-start'] . ",20");
} else {
$result2 = mysqli_query($connection, "SELECT * FROM candles WHERE approved=1 ORDER BY date " . $_GET['sort'] . " LIMIT " . $_GET['candle-start'] . ",20");
}
if(mysqli_num_rows($result2) > 0) {
$counter = 0;
while($row = mysqli_fetch_assoc($result2)) {
$counter = $counter + 1;
if($counter % 2 == 1){
echo "</div>";
echo "<div class='candle-row'>";
}
if($row["approved"] == 1 and $_GET['teacher'] == 'yes') {
continue;
}
echo "<div class='candle'><div class='candle-icon'>";
echo '<span class="fa-stack fa-4x"><i class="fa fa-square-o fa-stack-2x"></i><i class="fa fa-' . $row["candle_icon_type"] . ' fa-stack-1x" style="color: ' . $row["colour"] . '"></i></span></div>';
echo '<div style="display:table;height:100%;"><div style="display:table-cell;vertical-align:middle;"><div>';
echo "<h3><a href='candle.php?id=" . $row["id"] . "'>For " . ucwords($row["title"]) . "</a></h3>";
echo "<br>";
$result3 = mysqli_query($connection, "SELECT * FROM users WHERE id=" . $row['user_id'] . " LIMIT 1");
if(mysqli_num_rows($result3) > 0) {
while($row1 = mysqli_fetch_assoc($result3)) {
if($row["anonymous"] == 1 and $_SESSION["user_group"] == '1'){
echo "<h4><a href='candle.php?id=" . $row["id"] . "'>Shared by an Anonymous User<br>";
} else {
echo "<h4><a href='candle.php?id=" . $row["id"] . "'>Shared by " . $row1["username"] . "<br>";
}
}
} else {
echo "<h4><a href='candle.php?id=" . $row["id"] . "'>Shared by Unknown<br>";
}
echo "Shared " . ucwords(timeAgo($row["date"])) . "<br>" . $row['fuel'] . " Fuel</a></h4></div></div></div>";
echo "</div>";
}
}
echo "h8ktncuidabciafvabc7932hcueqbc73gd8713gdy713gd6831df8136fd13d";
if($_GET['teacher']) {
$result1 = mysqli_query($connection, "SELECT id FROM candles WHERE approved=0");
} else {
$result1 = mysqli_query($connection, "SELECT id FROM candles WHERE approved=1");
}
if(mysqli_num_rows($result1) == 0){
echo '0 Candles';
} elseif(mysqli_num_rows($result1) == 1){
echo '1 Candle';
} else {
echo mysqli_num_rows($result1) . ' Candles';
}
mysqli_close($connection);
?>
最佳答案
是的,EDGE
在您没有打开它时似乎确实错过了 console
对象(您称之为 DOM Explorer)。当您的 console
关闭时,它并不“存在”,因此 EDGE
会抛出一个您看不到的错误,因为您的 console
是关闭。
(听起来很奇怪?欢迎使用 Microsoft
创建的逻辑)!
我猜你在代码中使用了 console.log()
方法。因此,要么删除 console.log()
调用,要么将此解决方法放在 javascript 的顶部:
if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function () { };
关于php - Ajax PHP - 仅当 DOM Explorer 打开(边缘)时才会回显结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35638862/
我正在使用 @ConditionalOnProperty 创建一个 FileCompressor bean: @Bean @ConditionalOnProperty(prefix = "file.r
我是一名优秀的程序员,十分优秀!