nicolas merle
nicolas merle

Reputation: 41

Ajax Json request not working

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.

Upvotes: 3

Views: 387

Answers (3)

nicolas merle
nicolas merle

Reputation: 41

I finnaly manage to do it, In fact this jQuery code was good :

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

The only probleme was the plugin of wordpress I was using which was runing the php and displaying it into the main page. But then the answer was way more than json (html containing json) and this is why it didn't worked. But now it's fully working so thanks again for your help :)

Upvotes: 0

Theraloss
Theraloss

Reputation: 700

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.

Upvotes: 2

Tom Hunt
Tom Hunt

Reputation: 958

Your setInterval call is broken. You want setInterval(checkCount, 500) rather than setInterval("checkCount()", 500). The latter is passing a string, and even without the quotes it'd be calling the function and passing its return value; setInterval takes the function itself.

Upvotes: -1

Related Questions