I did a code for get the data from a remote url but nothing its work.
php page file:
<script>
$(document).ready(function () {
console.log('comecei');
$('#city').typeahead({
source: function (query, result) {
$.ajax({
url: "http://localhost:81/xxx/api/api.auto.complete.city.php",
data: 'query=' + query,
dataType: "json",
type: "POST",
success: function (data) {
console.log(data);
result($.map(data, function (item) {
return item;
}));
}
});
}
});
});
</script>
<input id="city" type="text" data-provide="typeahead" />
The file to load ajax data:
<?php
error_reporting(E_ALL);
header("Content-Type: text/html; charset=ISO-8859-1");
require_once ("../system/Database.php");
require_once ("../system/Site.php");
require_once ("../system/Functions.php");
$query = isset($_POST['query']) ? strval($_POST['query']) : '';
//not using query for while...
$db = new HiudeDatabase();
$items = $db->select("call sp_list_city()");
if (is_array($items))
{
foreach ($items as $item)
{
if (isset($item['id']) && isset($item['name']))
{
$result[] = json_encode($item);
}
}
echo json_encode($result);
}
$db->close();
?>
And here some examples of output of ajax file:
["{\"id\":\"4\",\"name\":\"Alegre\"}","{\"id\":\"5\",\"name\":\"Alfredo Chaves\"}"
But nothing works when I put some text on textbox in page, where is the problem ?
Other point, how can I debug the life cycle of this script ?