简体   繁体   中英

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!

$('#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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM