jquery auto建议使用ajax和django

时间:2013-11-25 08:47:04

标签: javascript jquery ajax django

我的jquery自动完成功能就像这样

$(function(){
     $("#id_q").autocomplete({
        //source: availableTags, This works.....(static list are working)
        source: function (request, response) {   // This is not working 
            $.ajax ({
                url: '/college-search/autosuggest/',
                data: { 'keyword':$('#id_q').val(), 'page' = 'searchbox'},
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset = utf-8",
                success: function (data) {
                    response (data.autosuggest);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                }

            });
        },
    minLength: 1,
    //open: function() { $(this).autocomplete("widget").css({"width": $(this).width()+6})},
    //appendTo:$( ".cls_comp").parent(),
    select: function( event, ui ) { }

    });
}

和我的django这样的观点 -

def auto_suggest(request):

    autosuggest = []
    s_query = ''
    spelling = ''
    letters = ''
    lenletters =''
    loc = ''

    baseurl = settings.HAYSTACK_SOLR_URL

    baseurl = baseurl + 'terms?terms.fl=autocomplete_text&omitHeader=true&terms.limit=5000&terms.sort=index&wt=json&terms.prefix='
    if request.GET.has_key('keyword'):
        letters = request.GET.get('keyword')
        letters = letters.lower()
        letters = letters.replace(' ', '+')
        lenletters = len(letters)
    print "==================="    

    targeturl = baseurl + letters

    print targeturl

    if letters != '':
        resp = requests.get(targeturl)
        text = resp.text.encode('utf-8')
        jsontext = json.loads(text)
        autosuggest =[x for x in jsontext['terms']['autocomplete_text'] if x!= 1]

    print autosuggest

    if request.GET.has_key('page'):
        webpage = request.GET.get('page')
        if webpage == 'searchbox':
            template = 'widgets/searchbox.html'
        elif webpage == 'searchboxlong':
            template = 'widgets/searchbox_long.html'
        else:
            template = ''

    if request.GET.has_key('spellcheck'):
        misspelled_word = request.GET.get('spellcheck')
        s_query = misspelled_word
        spelling = spell_check(misspelled_word)

    context = {
    'autosuggest' : autosuggest,
    'lenletters' : lenletters,
    'letters' : letters,
    'searches' : searches,
    'spelling' : spelling,
    'loc' : loc,
    }
    return render_to_response(
        template,context, context_instance=RequestContext(request)
    )

我的问题是当我在id = id_q的文本框中输入内容时,我无法进行ajax调用。

无法理解我做错了什么..... 任何帮助将深表赞赏

1 个答案:

答案 0 :(得分:1)

这里有语法错误:

data: { 'keyword':$('#id_q').val(), 'page' = 'searchbox'},

应该是

data: { 'keyword':$('#id_q').val(), 'page': 'searchbox'},

这没有任何意义:

response (data.autosuggest)

你想在那里做什么?你的意思是return data.autosuggest

另外,您可能不希望为Django响应呈现模板 - 它应该只是JSON。您至少应该发布'widgets / searchbox.html'的内容。