2010-11-13 127 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以前設置我有一個通用的名稱不起作用。感謝所有那些維護我的問題的人。