Bootstrap 2.2 Typeahead问题

当我在文本框中输入时,我使用以下代码获取建议列表。

JS

 $("#address").typeahead({ source: function(query,typeahead){ $.ajax({ url: "http://localhost/disc/autocomplete/"+query, type: "GET", dataType: "JSON", async: true, success: function(data){ typeahead.process(data); } }); }, property: 'address', items:8, onselect: function (obj) { // window.location = obj.url; } }); 

PHP

  $count=0; foreach ($query->result() as $row) { $count++; $item['value'] = $row->address; $item['id'] = $count; $output[] = $item; } echo json_encode($output); 

TextBox

  

现在,当我在文本框中输入时,我收到错误

 Uncaught TypeError: Object function (){return a.apply(c,e.concat(k.call(arguments)))} has no method 'process' 

编辑:

 $("#typeahead").typeahead({ source: function(query,callback){ $.ajax({ url: "http://192.168.8.132/disc/autocomplete/"+query, type: "POST", dataType: "JSON", async: false, success: function(data){ //this.process(data); callback(data); } }); }, items:8, onselect: function (obj) { // window.location = obj.url; } }); 

什么是打字? 在调用进程成员之前,你显然需要对它做些什么。 (instanciation,无论什么类型的应该是)。

编辑1:

 source: function(query,callback/** you need that to execute something after the XMLHttp request has returned**/){ $.ajax({ url: "http://localhost/disc/autocomplete/"+query, type: "GET", dataType: "JSON", async: true, success: function(data){ /** execute the callback here do whatever data processing you want before**/ callback(data); } }); }, 

在函数式编程中,它被称为continuation(如GOTO指令)。

编辑2:

你没有决定回调是什么,回调就是function,所以除了用你收到的数据调用它之外,不要尝试做任何事情。 同样, 回调是一个类似GOTO的指令,它是一个延续,你不能控制它。 你需要用data作为参数来执行它。