0

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 ?

MCunha98
  • 81
  • 3
  • 12
  • Can you please define "*not working*"? **What**, exactly, doesn't work? You appear to be able to generate the JSON successfully. From there are you asking how to write it to the page? That'a a simple `json_decode()`. – Obsidian Age Jul 27 '17 at 00:02
  • https://stackoverflow.com/questions/11435433/jquery-ui-autocomplete-with-json – Muhammad Usman Jul 27 '17 at 00:08
  • When I try to get the result in the suggestion, nothing happen. For example the console.log(data) statement not is fired in console window, its like the js not is fired – MCunha98 Jul 27 '17 at 00:18
  • https://s19.postimg.org/wr2h0aqub/Sem_t_tulo.png – MCunha98 Jul 27 '17 at 00:44

0 Answers0