简体   繁体   English

jQuery的自动建议与ajax和Django

[英]jquery auto suggest with ajax and django

I have my jquery autocomplete like this 我有这样的我的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 ) { }

    });
}

and my django views like this -- 和我的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)
    )

my problem is I am not able to make an ajax call when I type something in the text box with id= id_q . 我的问题是,当我在id = id_q的文本框中键入内容时,我无法进行ajax调用。

Not able to understand what I am doing wrong..... Any help will be deeply appreciated 无法理解我在做什么错.....任何帮助将不胜感激

You have a syntax error here: 您在这里遇到语法错误:

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

It should be 它应该是

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

And this makes no sense: 这没有任何意义:

response (data.autosuggest)

What are you trying to do there? 您想在那里做什么? Do you mean return data.autosuggest ? 您的意思是return data.autosuggest吗?

Plus you probably don't want to be rendering a template for the Django response - it should just be JSON. 另外,您可能不想为Django响应呈现模板-它应该只是JSON。 You should at least post the contents of 'widgets/searchbox.html'. 您至少应该发布“ widgets / searchbox.html”的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM