AJAX:Bootstrap Typeahead:意外的令牌错误

时间:2013-01-11 07:42:20

标签: jquery ajax twitter-bootstrap syntax-error

我在文档的第一个字母上收到意外的令牌错误。

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

在我的page.php中:

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

因此,使用此代码,我收到Uncaught Syntax: Unexpected token <

的错误

因此,当我删除<script></script>所以它只是echo "var query=".$json.";"时,我会Uncaught Syntax: Unexpected token v

所以我假设它只是继续给我意外的第一个字母的标记,这个字母是在页面上回显的.php

有人可以告诉我出了什么问题吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

$('#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);
相关问题