I have a php script which 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);
?>
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 am using Jquery to parse it but for some reason it is failing to parse. 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'm getting from my console is :
08:52:52.661 no element found1 CurrencyQuery.php:24:4
This basically refers to my PHP script returning the json. I don't have any ideas why this isn't working. I have validated the JSON with an online validator and it seems ok and the Jquery, it should be ok. Could anyone give me a clue ?
Thank you all, I figured out the solution. I had to wrap the JSON encoded data from my php script in a callback function. I could retrieve the data then. I'm not sure whether this is the best way to deal with this issue but so far it works :)