weixin_33682790 2015-09-09 15:24 采纳率: 0%
浏览 55

Ajax Json请求不起作用

So I'm juste trying to create a counter which update a value in my database everytime I click on a button and then I would like to display this value on the page. My code to update the value is working, but my code to display the value doesn't want to work.

Here is my js code to refresh the counter :

<script>
setInterval("checkCount()",500); 
function checkCount()
{
    jQuery.ajax({
        url: 'http://ntc.aiesecfrance.org/server_count_get/',
        dataType: 'json',
        success: function(data) {
           alert("data.count");       
        }
    });
};
</script>

and here is my php code to get the value from the database.

$connection = mysqli_connect("localhost", "test", "", "test");
  if (mysqli_connect_errno()) {
      echo 'connection failed';
  }
// Fetching the value
$query  = "SELECT count FROM counterval WHERE id=1";
$count = mysqli_query($connection,$query);
$value = mysqli_fetch_array($count);

header('Content-type: application/json');
echo json_encode($value);
?>

but with this I got no alert. It just doesn't show up. Here is an example of what is retunrned by the php :

{"0":"8","count":"8"}

Thanks in advance for your help.

  • 写回答

3条回答 默认 最新

  • weixin_33744854 2015-09-09 15:29
    关注

    You have to remove the quotes.

    It will be:

    alert(data.count)

    EDIT

    Try this.

    jQuery.get('http://ntc.aiesecfrance.org/server_count_get/', function(r) {
        var json = $.parseJSON(r);
        alert(json.count);  
    });
    

    I hope you included jQuery. Anyway, try also to put the function inside

    $(function() {
       function checkCount() { ... }
    });
    

    EDIT 2 (IMPORTANT)

    I saw that the output is an HTML page, not only the JSON. Try this.

    $.get('http://ntc.aiesecfrance.org/server_count_get/', function(r) {
            var $html = $(r);
            var json_value = $html.find('#post-291 .entry-content').text();
            var json = $.parseJSON(json_value);
            alert(json.count);  
        });
    

    Anyway you can't do cross-origin calls.

    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛