hellomello
hellomello

Reputation: 8597

AJAX: Bootstrap Typeahead: Unexpected Token Error

I'm getting unexpected token error on the first letter of what is in my document.

$('#typeahead').typeahead({
source: function (typeahead, query) {
    return $.post('ajax/page.php', { query: query }, function (data) {
        alert(data);
        return typeahead.process(JSON.parse(data));
    });
}
});

In my page.php:

<?php 
        $array[] = array("test","treat","food");
        $json = json_encode($array);
        echo "<script>var query = ".$json.";</script>";
?>

So with this code, I get an error with Uncaught Syntax: Unexpected token <

So when I remove <script></script> so it'll just echo "var query=".$json.";", I get Uncaught Syntax: Unexpected token v.

So I'm assuming it'll just keep giving me unexpected token of the first letter that is being echo'd out of page.php

Can someone tell me what is wrong?

Thanks!

Upvotes: 0

Views: 1595

Answers (1)

cetver
cetver

Reputation: 11829

$('#typeahead').typeahead({
source: function (query, process) {
    return $.post('ajax/page.php', { query: query }, function (data) {
        process(JSON.parse(data));
    });
}
});

//page.php
$array = array("test","treat","food");
echo json_encode($array);

Upvotes: 2

Related Questions