I have a ajax fucntion that gets a json array from the php , 8 numbers formed like this ["0","0","0","0","0","0","0","1"] , As I get this in a var called data , data[0] and this kind doesen't work , how can I access this numbers ?

Recommended Answers

All 3 Replies

Try something like this...

<script>
$.ajax({
    dataType: "json",
    url: "getJson.php",
    success: function (data) {
       for (var index = 0; index < data.length; index++) {
          console.log(data[index]);
       }
    }
 });
</script>

You can see the results using your browser's dev tools, console.

1322d0546996dd704f1826485e46c7ab

Member Avatar for diafol

show your php code to see if you're echoing it out properly in json format

Try the following example:

In index.php

 <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(function() {
    $.ajax({
    dataType: "json",
    url: "json.php",
    success: function (data) {
        for (var index = 0; index < data.length; index++) {
            alert(data[index]);
        }
    }
    });

});
</script>

In json.php

<?php
$array = array("0","1","2","3","4","5","6","7","8");
echo json_encode($array);
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.