I’m trying to make a PhoneGap application that gets data from MYSQL with PHP getJSON function and then posts it as a list on the application. The list part works just fine, but I would like to inform user somehow if there is no data on the server.
Now it just shows nothing and because I’m trying to develope a mobile application the user might just think that the connection is slow and the application is still trying to get the data even though there is nothing to get.
This is the code I use:
function getEmployeeList() {
$.getJSON(serviceURL + 'getemployees.php', function(data) {
if(JSON.stringify(data) != JSON.stringify(oldData)){
$('#employeeList li').remove();
employees = data.key;
$.each(employees, function(index, employee) {
$('#employeeList').append('<li><a href="keikka.html?id=' + employee.IND + '">' +
'<h4>' + employee.OSO + '</h4>' +
'<img src="pics/' + employee.TILA + '.png"/>' +
'<p>' + employee.AIKA + '</p>' +'</a></li>');
});
$('#employeeList').listview('refresh');
if(oldData != "")
alert("New data!");
oldData = data;
}
});
}
How can I make it so that when json looks like this {"key":[]} it posts a text like “No data”
Retrieve the data from the server using some ajax call. Check the data on the client side (of let your server return some error code) so you know that there is no data to be displayed. If this is the case, show a no data message or whatever suits best to the user.
Something like: