0

嗨,我正在使用 jQuery UI Autocomplete - 我能够发送正确的请求,并且还可以按预期接收数据(以 json 格式)。但是它无法显示列表,我收到一个错误,说“a未定义,然后指向主jQuery文件。以前有没有人遇到过这个问题,因为我不知道如何找到解决方案。这是我的自动完成jQuery:

$(document).ready(function () {
    $("#city").autocomplete({
        source: function (request, response) {
            var searchTerm = ("{\"getSuggestions\":" + "{\"query\"" + ":" + "\"" + request.term + "\"}}");
            $.ajax({
                url: userNameUrl,
                type: "POST",
                //contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: searchTerm,
                success: function (data) {
                    response($.map(data.result, function (item) {
                        return {
                            label: item.name,
                            value: item.name
                        }

                    }));
                }

            });
        },
        minLength: 2
    });
});

这里还有一个从服务器返回的 JSON 数据示例:

{"suggestionList":[{"imgUrl":"image?file=530d4e64-ebfe-4e47-af89-5d74c0f2e906.png","name":"Canon IXUS 970"},{"imgUrl":"image?file=c963a27e-8762-49f7-a13f-b7d4e9b96ad0.png","name":"Canon IXUS 870"},{"imgUrl":"image?file=8093f2ff-9689-496e-9c01-803ea7cc596d.png","name":"Canon IXUS 300"},{"imgUrl":"image?file=b0246291-dc1c-47df-9082-b457237f8143.png","name":"Canon IXUS 105"},{"imgUrl":"image?file=36bc45c9-7d2a-454e-90ae-c70bccfa646a.png","name":"Canon IXUS 130"},{"imgUrl":"image?file=10758f92-7801-4ca7-b472-507be9f608c7.png","name":"Canon PowerShot SX120"},{"imgUrl":"image?file=90c30231-13d1-45d6-9f5d-7dbdae1f79bf.png","name":"Canon PowerShot SX210"},{"imgUrl":"image?file=8ffab8fc-b71a-4c5a-b1cc-0cbd1db0aebf.png","name":"Canon PowerShot G10"},{"imgUrl":"image?file=af8b415e-3d5c-4e5c-8b56-c2f2bd33ff0a.png","name":"Canon PowerShot A3000"},{"imgUrl":"image?file=88490780-a7b6-428e-80d4-d26c10343908.jpg","name":"Canon IXUS 1000 HS"},{"imgUrl":"image?file=cd89e47d-7ee8-49a5-b986-116231f64061.jpg","name":"Canon Powershot SX130 IS"},{"imgUrl":"image?file=4240f3a4-c1fa-4798-8e40-68991f7af121.jpg","name":"Canon PowerShot S95"},{"imgUrl":"image?file=3758c782-c059-450c-92fa-36aa61e8bb0a.jpg","name":"Canon PowerShot S95"},{"imgUrl":"image?file=42cecc35-2773-45fb-a556-3fa1905fe454.png","name":"Canon PowerShot SX1 IS"},{"imgUrl":"image?file=fde59ede-10cd-4c7d-a691-7e5eeac41b6b.png","name":"Canon Power Shot S90"},{"imgUrl":"image?file=c05f1582-edbb-49d2-a373-93a10035462b.png","name":"Canon PowerShot S3 IS"},{"imgUrl":"image?file=49fb81b9-840e-4809-97df-28c223496d9a.png","name":"Canon PowerShot Pro 1"},{"imgUrl":"image?file=7c2680bd-b3ae-45f8-8a8c-dd66b1d82b35.png","name":"Canon PowerShot D10"},{"imgUrl":"image?file=f7e294aa-ea80-4303-bfa2-25c55f46b604.png","name":"Canon PowerShot A580"},{"imgUrl":"image?file=2819e7d2-d99f-49f5-bc7d-d522c0da055a.png","name":"Canon PowerShot A480"},{"imgUrl":"image?file=057aad36-f6d4-4cb4-a7b7-5b1aef0a4be7.png","name":"Canon PowerShot A470"},{"imgUrl":"image?file=e73fa7d5-a265-4ea7-9e64-ebbb0832ea8f.png","name":"CANON POWERSHOT A3100 IS"},{"imgUrl":"image?file=c4229767-f943-4758-9dfb-bdf07a825850.png","name":"Canon PowerShot A1100 IS"},{"imgUrl":"image?file=a9f51021-ac55-457b-877f-ab84220596c4.png","name":"Canon EOS Digital Rebel"},{"imgUrl":"image?file=79f36b6d-16dc-4647-8640-8fb403b6afbd.png","name":"Canon EOS D60"},{"imgUrl":"image?file=645aaaa3-ff2b-4493-b3f5-6b261556a199.png","name":"Canon EOS 5D"},{"imgUrl":"image?file=b2ce66c7-ed34-4345-b338-bf64290cd9e8.png","name":"Canon EOS 350D"},{"imgUrl":"image?file=47624425-cd2e-47d3-ae73-e8d3645e3f56.png","name":"Canon EOS 30D"},{"imgUrl":"image?file=87d7a6e1-1ed8-41d7-8e20-8602b6b5d603.png","name":"Canon EOS 20D"},{"imgUrl":"image?file=1e9938fd-c13a-409a-a157-6cc5f4c84190.png","name":"Canon EOS 10D"}]}

任何帮助将不胜感激。

问候乔纳森

4

2 回答 2

1

嗨,以防万一其他人有同样的问题,错误是在这里引起的:

response($.map(**data.result**, function (item) {

它应该是:

response($.map(**data.suggestionList**, function (item) {

如您所见,suggestList 是 JSON 集的名称,之前我有一个通用名称,但它不起作用。感谢所有看过我问题的人。

于 2010-11-13T21:48:46.653 回答
0

您正在使用userNameUrl而没有先定义它。jQuery 可能正在尝试访问a.url,但找不到它,所以它会抱怨。a最有可能$.ajax()在执行时在内部存储参数。

于 2010-11-13T19:49:57.683 回答