0

按照自动完成的官方示例,我想出了这个。

$("#search").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "/search",
                    dataType: "jsonp",
                    data: {
                        featureClass: "P",
                        style: "full",
                        maxRows: 12,
                        name_startsWith: request.term
                    },
                    success: function (data) {
                        response($.map(data.username, function (item) {
                            return {
                                label: item.name,
                                value: item.name
                            };
                        }));
                    },
                    error: function (data) {

                    }
                });
            },
            minLength: 2,
            select: function (event, ui) {

            },

        });

但是我从来没有打过,success: function (data) { .. }但总是打error: function (data) {

Firebug 显示错误:"parsererror", SyntaxError: invalid label

在此处输入图像描述

/searchurl 在服务器端生成:

def search():
    data = None
    if 'name_startsWith' in request.args:
        q = request.args['name_startsWith']
        data = User.query(ndb.AND(User.firstname >= q, User.firstname <= q + u'\ufffd')).fetch(12)
        js = []
        for user in data:
            js.append({'name' : user.fullname()})         
    return jsonify(username=js)

请问有什么建议吗?

4

1 回答 1

1

将数据类型更改为,json因为您的响应是 json 对象,并且请求被发送到同一个域,因此不需要 jsonp

$("#search").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "/search",
                    dataType: "json",
                    data: {
                        featureClass: "P",
                        style: "full",
                        maxRows: 12,
                        name_startsWith: request.term
                    },
                    success: function (data) {
                        response($.map(data.username, function (item) {
                            return {
                                label: item.name,
                                value: item.name
                            };
                        }));
                    },
                    error: function (data) {

                    }
                });
            },
            minLength: 2,
            select: function (event, ui) {

            },

        });

演示:错误 演示:解决方案

于 2013-08-10T14:31:46.150 回答