ajax轮询

来源:互联网 发布:js改变radio值 编辑:程序博客网 时间:2024/06/03 23:43
<script type="text/javascript">        var getting = {        url:'server.php',        dataType:'json',        success:function(res) {         console.log(res);}};//关键在这里,Ajax定时访问服务端,不断获取数据 ,这里是1秒请求一次。window.setInterval(function(){$.ajax(getting)},1000);</script>PHP
<?php$pdo = new PDO('mysql:dbname=test;host=127.0.0.1','root','root');$resource = $pdo->query('select * from t1');$result = $resource->fetchall();if ($result) {    //exits data    print_r(json_encode(array('success'=>'存在数据')));    exit();}print_r(json_encode(array('failed'=>'不存在数据')));exit();?>

原创粉丝点击