1

我在Google App Engine應用程序中使用引導鍵入提示,但它不起作用。谷歌應用引擎中的自動完成與引導鍵入提示

我的HTML:

<div class="well"> 
    <input type="text" class="span3" id="search" name="search" 
     data-provide="typeahead" data-items="4" /> 
</div> 
<script> 
    $('#search').typeahead({ 
    ajax: { url: '/SearchCity', 
    triggerLength: 1 } 
}); 
</script> 

我的Python代碼:

class SearchCity(webapp2.RequestHandler): 
    def post(self): 
     data = ['cat','dog','bird', 'wolf'] 
     data = json.dumps(data)   
     self.response.out.write(data) 

爲什麼不工作的autocompletetion?這段代碼有什麼問題?

回答

2

typeahead希望選擇在options變量中。將您的看法以這樣的:

class SearchCity(webapp2.RequestHandler): 
    def post(self): 
     data = ['cat','dog','bird', 'wolf'] 
     data = json.dumps({'options': data}) #Changed line 
     self.response.out.write(data) 

而且,你的HTML可能需要一些改變,請嘗試以下操作:這裏

$('#id_chain').typeahead({ 
    minLength: 1, 
    source: function (query, process) { 
    return $.get('/SearchCity', function (data) { 
     return process(data.options); 
    }); 
    } 
}); 

聲明的是,你沒有指定要使用的引導版本。 ..這是與框架的V2 +。

+0

非常感謝您的回答,但我越來越「遺漏的類型錯誤:無法調用未定義的方法「toLowerCase」錯誤 – fledgling

+0

你能否提供一些上下文到該錯誤,可能是堆棧跟蹤?您可能會以不好的方式訂購您的腳本,但如果沒有更多信息,很難分辨。 – jro

+0

我得到這個錯誤在JavaScript控制檯 遺漏的類型錯誤:無法調用未定義new.js的方法 'toLowerCase':205個 Typeahead.matcher new.js:205 (匿名函數)new.js:192 e.extend。 grep jquery-1.7.1.min.js:2 Typeahead.lookup new.js:191 Typeahead.keyup new.js:302 g jquery-1.7.1.min.js:2 f.event.dispatch jquery -1.7.1.min.js:3 h.handle.i – fledgling

1

由於bootstrap's documentation說,這前人的精力看起來更不像:

function get_data_async(query, callback) { 
    $.ajax({ 
      url: '/SearchCity/', 
      data: query, 
      dataType: 'json', 
      type: 'POST', 
      success: callback 
    }) 
} 

$('#search').typeachead({source: get_data_async}) 
+1

非常感謝您的解釋,我已經解決了這個問題在SearchCity中添加'self.response.headers ['Content-Type'] ='application/json'' – fledgling