I've been having an issue with AJAX parsing a JSON Array from a webservice I'm creating. My front end is a simple ajax & jquery combo to display results returned from the webservice I'm creating.
I'm getting an error within Chrome's console stating "cannot read the length property of undefined" despite knowing that there are results from my database query.
After looking for an answer for days I still cannot figure out why I get the console error.
Thank you for any help! :)
function ajaxrequest(e)
{
var r = $('#region').val();
var t = $('#type').val();
console.log(r,t);
$.ajax('https://URL...../.../POI/POI_LOOKUP.php',
{ type: 'GET',
data: 'type='+t+'®ion='+r+'&format=json',
success: onSuccess }
);
}
function onSuccess(data,status,xmlHTTP)
{
var html = "<table><th>name</th><th>type</th><th>country</th><th>region</th>";
for(var i=0; i<data.length; i++)
{
html = html + '<tr><td>' + data[i].name + '</td>' + '<td>' + data[i].type + '</td>' + '<td>' + data[i].country + '</td>' + '<td>' + data[i].region + '</td></tr>';
}
html = html + '</table>';
$('#results').html(html);
console.log(data);
console.log(status);
}
Here is my PHP to search and return all results:
IF ($type == "any" && !isset($region)) /* Search DB for all types of POI for all regions*/
{
$statement = $conn->prepare("SELECT * FROM pointsofinterest;");
$statement->execute();
$row = $statement->fetch();
if ($row == false)
{
header("HTTP/1.1 204 No Content");
}
else
{
$allResults = array();
while($row != false)
{
$allResults[] = $row;
$row = $statement->fetch(PDO::FETCH_ASSOC);
}
echo json_encode($allResults);
}
}
console.log(data);at the start ofonSuccess?