1

我正在尝试使用 jqueryUI 自动完成功能,其中可用的标签将从后端源中获取。这是我的代码。

代码

<div class="span4 pull-right" id="search">
    Search : <input type="text" placeholder="Search">
</div>

js代码

<script>
    $(function() {
    $( "#search" ).autocomplete({
    source: "/dashboard/search"
    });
});
</script>

** django views.py **

def search(request):
    availableTags = ["ActionScript","AppleScript","Asp","BASIC","C","C++","Clojure","COBOL","ColdFusion","Erlang"];
    ctx = {"availableTags":availableTags}
    return HttpResponse(availableTags) #returns the set of values(checked with firebug while debugging) but autocomplete doesnot works.

    #return render(request, 'dashboard/dashboard.html', ctx) // returns nothing.

仪表板 urls.py

urlpatterns = patterns('modules.energy.dashboard.views',
    url(r'^$','dashboard',name='cilantro_dashboard'),
    url(r'search','search',name='cilantro_search'),
)

主要网址.py

urlpatterns = patterns('',

    url(r'^dashboard/', include('modules.energy.dashboard.urls')),
)

这是我在使用 firebug 调试时收到的响应。

ActionScriptAppleScriptAspBASICCC++ClojureCOBOLColdFusionErlang

availabletags的被​​退回,但自动完成仍然不起作用。我哪里错了?

4

2 回答 2

1

似乎您需要在发送数据 dict 之前将其转换为 JSON。

from django.utils import simplejson
data = simplejson.dumps(availableTags)
return HttpResponse(data)
于 2013-10-19T15:34:17.733 回答
0

据我所知,如果您返回的结果是正确的,那可能只是因为您divinput.

于 2013-10-19T14:16:48.257 回答