2013-01-11 35 views
0

我在我的文檔中的第一個字母上收到意外的令牌錯誤。AJAX:Bootstrap Typeahead:意外令牌錯誤

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

所以我假設它只會不斷給我,被回顯出來page.php文件

誰能告訴我什麼是錯的第一個字母的意外的標記?

謝謝!

回答

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); 
+0

這給了我'進程沒有定義',所以我沒有'返回typeahead.process(data)',它只是返回第一個字母重複該字母幾次。 – hellomello

+0

你使用哪種boostrap版本?在版本2.2.2(最新)中的所有作品。工作演示:http://www.filehosting.org/file/details/410520/trash.7z – cetver

+0

太棒了!有效!我忘了改變參數'(查詢,進程)'。另外,我不得不從'$ array []'中刪除'[]'。這是爲什麼?謝謝! – hellomello