I would like to know what is the latest JQuery syntax for parsing a JSON response by array index from a HTTP GET or POST request. My server side works great so I am look for way to parse the table row returned by PHP.
Here is my SERVER SIDE PHP code:
function qry_select_account($pk_account) {
// Global variables
Global $db_host, $db_user, $db_pass, $db_name;
// Connect to database server
$dbc = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error());
// Select target database
mysql_select_db($db_name) or die(mysql_error());
// suchislife801 <--- Selects account information
// Run query
$sql_qry = mysql_query("SELECT * FROM tblaccount
WHERE pk_account = '$pk_account'") or die(mysql_error());
// SQL Criteria AND acc_confirmed = 'y' AND acc_locked = 'n'
// Fetch table row for this user
$row = mysql_fetch_row($sql_qry);
print json_encode($row);
//echo 'Account: ' . $row[0];
//echo '<br />';
//echo 'Passowrd: ' . $row[1];
//echo '<br />';
//echo 'Acc Level: ' . $row[2];
//echo '<br />';
//echo 'Email: ' . $row[3];
//echo '<br />';
//echo 'Language: ' . $row[4];
//echo '<br />';
//echo 'Time Zone: ' . $row[5];
//echo '<br />';
//echo 'Signup: ' . $row[6];
//echo '<br />';
//echo 'Conf Code: ' . $row[7];
//echo '<br />';
//echo 'Confirmed: ' . $row[8];
//echo '<br />';
//echo 'Locked: ' . $row[9];
mysql_free_result($sql_qry);
// Close Connection
mysql_close($dbc);
}
Here is my JQuery code which is not working.
<script type="text/javascript">
$(function(){
$('#test_btn').live('click', function() {
$.getJSON("http://localhost/api.php?call=select_account&p0=suchislife801", function(json){
alert("JSON Data: " + json);
});
});
</script>
You can use jQuery.parseJSON( json ) function. Make sure that json returned from server is a valid json
You can validate json response from here
Use following to check if error is returned by server
$.getJSON("example.json", function(data) {
alert(date);
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
);
looping through json depends on type of json
if its a map type structure, like following
var map = {
"data1": "value1",
"data2": "value2"
};
you can use following to iterate through
$.each(map, function(key, value) {
alert(key + ': ' + value);
});
Reference: http://api.jquery.com/jQuery.each/
console.log(json)
- binarious 2012-04-04 20:00