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 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab