【问题标题】:typeahead.js user query in utf-8utf-8 中的 typeahead.js 用户查询
【发布时间】:2014-07-29 11:57:58
【问题描述】:

我和这里回答的问题相同:Transliteration in typeahead.js

但我不能在答案中使用解决方法,因为建议列表是动态的! 有没有办法让用户在查询中使用非 ascii 字符(é、à、è、ë等)?

【问题讨论】:

  • This 回答可能有帮助吗?

标签: character-encoding typeahead.js


【解决方案1】:

这是找到的解决方案:

HTML

$('.typeahead').typeahead(
        {
            minLength: 3,
        },
        {
            name: 'place',
            displayKey: 'city',
            source: function (query, process) {
                return $.get("{% url 'profil:search_place_request' search_term='user-query' %}".replace('user-query', query), { query: query }, function (data) { 
                    objects = [];
                    $.each(data, function(i, object) {
                        objects.push({
                            city : object.city,
                            zipcode : object.zipcode,
                            id : object.id,
                        });
                    });
                    return process(objects);
                });
            }
        }
    );

查看

@login_required
def search_place_request(request, search_term):
    search_term = search_term.encode("utf-8")
    response, response_dict = selfcare.libs.core.utils.MyAPIRequests.get("place/?term={}".format(search_term))
    if response.ok :
        log.info('[profil] request GET places. Search term : {},  USER : {}'.format(search_term, request.user.username))
        return HttpResponse(json.dumps(response_dict['results']), content_type="application/json") 
    else :
        log.warning('[profil] request DELETE customer address failed. Status code : {}, USER: {}'.format(response.status_code, request.user.username))
        return HttpResponse("", content_type="application/json") 
    return HttpResponse("", content_type="application/json")

在我看来,我明确编码为 utf8,它成功了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 2015-12-05
    相关资源
    最近更新 更多