2010-11-13 92 views
0

您好我正在使用jQuery UI自动完成 - 我能够发送正确的请求,并且还按照预期收到数据(以json格式)。然而,它不能显示列表,我得到一个错误,说“a是undefined然后指向主jQuery文件。有没有人遇到过这个问题,因为我失去了我如何找到解决方案。我自动完成的jQuery:jQuery UI AutoComplete发生奇怪的错误

$(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"}]} 

任何帮助,将不胜感激

问候乔纳森

回答

0

您正在使用userNameUrl而未先定义它。 jQuery可能试图访问a.url,但它无法找到它,所以它抱怨。 a很可能在执行时在内部存储$.ajax()的参数。

+0

没有抱歉我没有粘贴所有的代码userNameUrl实际上是在真正的脚本中定义的。感谢您的帮助,虽然 – jonnyhitek 2010-11-13 21:49:48

1

嗨,以防任何其他人有错误在这里造成了同样的问题:

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

它应该阅读:

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

正如你可以看到suggestionList是的名称JSON以前设置我有一个通用的名称不起作用。感谢所有那些维护我的问题的人。