jquery长轮询不显示任何内容
出于某种原因,下面的内容根本没有显示任何内容——你知道我做错了什么吗 更新1: index.phpjquery长轮询不显示任何内容,jquery,long-polling,Jquery,Long Polling,出于某种原因,下面的内容根本没有显示任何内容——你知道我做错了什么吗 更新1: index.php $(document).ready(function(){ (function poll(){ $.ajax({ url: \"getDbData.php\", success: function(data){ document.write(data.timestamp); }, dataType: \"json\", complete: poll, timeout
$(document).ready(function(){
(function poll(){
$.ajax({ url: \"getDbData.php\", success: function(data){
document.write(data.timestamp);
}, dataType: \"json\", complete: poll, timeout: 30000 });
})();
});
<?php
echo "
<html>
<head>
<script src=\"jquery.min.js\" type=\"text/javascript\" charset=\"utf-8\"></script>
<script type=\"text/javascript\">
$(document).ready(function(){
(function poll(){
$.ajax({ url: \"getDbData.php\", success: function(data){
var json = eval('('+data+')');
if json['timestamp'] != \"\" {
document.write(json['timestamp']);
}, dataType: \"json\", complete: poll, timeout: 30000 });
})();
});
</script>
</head>
<body>
</body>
</html>
"; //end of echo
?>
getDbData.php
$response = array();
$response['timestamp'] = time()+microtime();
echo json_encode($response);
sleep(1);
<?php
$response = array();
for ($testLoop=0; $testLoop < 100; $testLoop++) {
$response['timestamp'] = time()+microtime();
echo json_encode($response);
sleep(1);
}
?>
原始代码: index.php
$(document).ready(function(){
(function poll(){
$.ajax({ url: \"getDbData.php\", success: function(data){
document.write(data.timestamp);
}, dataType: \"json\", complete: poll, timeout: 30000 });
})();
});
<?php
echo "
<html>
<head>
<script src=\"jquery.min.js\" type=\"text/javascript\" charset=\"utf-8\"></script>
<script type=\"text/javascript\">
$(document).ready(function(){
(function poll(){
$.ajax({ url: \"getDbData.php\", success: function(data){
var json = eval('('+data+')');
if json['timestamp'] != \"\" {
document.write(json['timestamp']);
}, dataType: \"json\", complete: poll, timeout: 30000 });
})();
});
</script>
</head>
<body>
</body>
</html>
"; //end of echo
?>
ajax请求在请求完成之前不会“完成”,在您的情况下,请求需要100秒才能完成,因此它将始终达到30秒的超时。我删除了for循环,但仍然没有输出…?很可能是由于json无效而导致错误处理程序。一旦您通过返回有效的json停止命中错误处理程序,它将在这一行失败var json=eval(“(“+data+”)”)
因为数据
已经被解析为一个对象,所以它不是一个json字符串。那么,如何显示数据对象呢?我尝试了document.write(data.timestamp);但那仍然没有输出。正如所写的,你永远不会到达那一行。你修改过吗?