Hello All,
I'm trying to GET values from a DB table in JSON format through AJAX and then parse them into a JQuery List.
test.php (partial)
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
JSON Format it Returns:
[{"name":"1423 Peacock Haven"},{"name":"9835 Fredericksburg Rd"},{"name":"Test Project"}]
Following along with this tutorial, I'm trying to parse my own JSON above but nothing is returned in the list. I feel I may be all over the map here with errors - I'm not very familiar with AJAX as you can tell. I am also wondering if the issue is in my JSON as it does not have an object name like 'results' in the tutorial. Here is my modified code based upon the tutorial:
<script type="text/javascript">
function appReady(){
var ajax = new XMLHttpRequest();
ajax.open("GET","http://somobetech.com/test.php",true);
ajax.send();
ajax.onreadystatechange=function(){
if(ajax.readyState==4 && (ajax.status==200)){
console.log(json_results);
// Need to add UL on AJAX call or formatting of userlist is not displayed
$('#twitList').append('<ul data-role="listview"></ul>');
listItems = $('#twitList').find('ul');
$.each(json_results.results, function(key) {
html = '<center>'+json_results.results[key].name+'<center>';
listItems.append('<li>'+html+'</li>');
});
// Need to refresh list after AJAX call
$('#twitList ul').listview();
}
}
}
document.addEventListener("deviceready", appReady, false);
</script>
Note: I am retrieving the JSON differently, based upon this tutorial.
Any help you can provide pointing me in the right direction would be greatly appreciated!