I have a php script that returns the following json from my SQL Server:
<?php $server = "DEVTEST-PC\\SRVCLT"; $options = array("UID"=>"sa","PWD"=>"1234","Database"=>"Test"); $conn = sqlsrv_connect($server, $options); if ($conn === false) die("<pre>".print_r(sqlsrv_errors(), true)); //echo "Successfully connected!"; $result = sqlsrv_query($conn,"SELECT Currency, USDRate FROM Pax.CurrencyRate WHERE GBPRate BETWEEN 80 AND 800;"); if($result === false) { die( print_r( sqlsrv_errors(), true) ); } while( $row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC) ) { $myArray['paxcurjson'][] = $row; } echo json_encode($myArray); ?>
The output is as follows:
{"paxcurjson":[ {"Currency":"AFN","USDRate":49.5}, {"Currency":"ALL","USDRate":103.567}, {"Currency":"BDT","USDRate":77.562}, {"Currency":"DZD","USDRate":79.6146}]}
I use jquery to parse it, but for some reason it does not understand. My code is as follows:
<script type = "text/javascript" language = "javascript" > var url = 'CurrencyQuery.php'; $.getJSON(url, function(data){ for (i = 0; i < data.paxcurjson.length; i++) { console.log(data.paxcurjson[2].Currency); }; }); </script>
The only error message I get from the console is:
08: 52: 52.661 item not found1 CurrencyQuery.php: 24: 4
This basically refers to my PHP script returning json. I have no idea why this is not working. I checked JSON with an online validator, and it seems like OK and jQuery, everything should be fine. Can someone give me a hint?