简体   繁体   中英

Using jQuery to get the data of a JSON Object

I am new to jQuery and JSON. I have the following PHP code (getData.php) performs query from the database:

<?php
     header('Content-Type: application/json');
     ....
     // some code here
     ....
     $my_arr=array();
     // fectching data into array
     while($info = mysqli_fetch_array($result))
     {
       // convert to integer value if there is a bug after passing json_encode
       $rev=intval($info['bIRevNum']);
       $name=$info['bIName'];
       echo "<tr>";
       echo "<td>" . $info['bName'] . "</td>";
       echo "<td>" . $info['bRevNum'] . "</td>";
       echo "<td>" . $info['bIName'] . "</td>";
       echo "<td>" . $info['bIRevNum'] . "</td>";
       $my_arr[]=array('br'=>$name,'rev'=>$rev);
       echo "<td>" . $info['pName'] . "</td>";
       echo "<td>" . $info['pRevNum'] . "</td>";
       echo "</tr>";
     }

     // json encode
     echo json_encode($my_arr);
?>

After use echo 'json_encode' here I can see the JSON object under this format [{"br":"itemsb1","rev":37},{"br":"itemb2","rev":45}] on my page.

Now I want to access the integer of rev element of the object (37 and 45) for future usage by jQuery in a different PHP file, lets call it index.php and with the below script

<html>
.....
<script>
   $(document).ready(function(){
       $("button").click(function(){
           $.getJSON("getData.php", function(obj) {
             $.each(obj, function(key, value){
                    $("#div1").append("<li>"+value.rev+"</li>");
         });
       });
   });

    });
</script>   
...
// test here
<!---jquery--->
<div id="div1"><h2>CHANGE >>>> ....!!!!</h2></div>
<button>Calling from different PHP file</button>
</html>

If it is correct, when I click on the button "Calling from different PHP file" it should appears the value of JSON object as 37, 45.

I have tried many ways, but it does not display anything on my page.

Please help me with this!

It appears your problem is that you are echo'ing the html as well as the JSON. try removing the 'echo' from these lines

echo "<tr>";
echo "<td>" . $info['bName'] . "</td>";
echo "<td>" . $info['bRevNum'] . "</td>";
echo "<td>" . $info['bIName'] . "</td>";
echo "<td>" . $info['bIRevNum'] . "</td>";
echo "<td>" . $info['pName'] . "</td>";
echo "<td>" . $info['pRevNum'] . "</td>";
echo "</tr>";

DO NOT delete this line:

$my_arr[]=array('br'=>$name,'rev'=>$rev);

Also make sure your javascript is syntax correct

$("button").click(function(){
   $.getJSON("getData.php", function(obj) {
        $.each(obj, function(key, value) {
            $("#div1").append("<li>"+value.rev+"</li>");
        });
    });
});

Ensure that the only content coming back from getData.php is JSON-formatted; otherwise, it won't be parsed correctly. If you visit getData.php in your browser directly, you should only see JSON content, and nothing else (including errors, warnings, etc.). Your jQuery looks good; so the issue would have to be whatever content is coming back from the PHP script. I just whipped up a trivial test case using this PHP:

<?php
    header('Content-type: application/json');
    $my_arr[]=array('br'=>'something','rev'=>'2.0.5');
    echo json_encode($my_arr);
?>

Using the exact HTML you provided, that works just as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM