2012-04-16 52 views
0

我想從jqueryUI使用自動完成。當我提醒我的迴應我得到以下內容:([ { "id": "test", "label": "test", "value": "test" } ]);jQuery UI自動完成jsonp映射響應

但當我嘗試映射結果下拉結果爲空。這裏是我的代碼:

<script> 
$(function() { 
    function log(message) { 
     $("<div/>").text(message).prependTo("#log"); 
     $("#log").scrollTop(0); 
    } 

    $("#city").autocomplete({ 
     source: function(request, response) { 
      $.ajax({ 
       url: "http://localhost/jQuery/development-bundle/demos/autocomplete/search3.php", 
       jsonp: "jsonp_callback", 
       data: { 
        featureClass: "P", 
        style: "full", 
        maxRows: 12, 
        name_startsWith: request.term 
       }, 
       success: function(data) { 
        alert(data); 
        response($.map(data, function(item) { 
         return { 
          label: item.label, 
          value: item.value 
         } 
        })); 
       } 
      }); 
     }, 
     minLength: 2, 
     select: function(event, ui) { 
      log(ui.item ? 
       "Selected: " + ui.item.label : 
       "Nothing selected, input was " + this.value); 
     }, 
     open: function() { 
      $(this).removeClass("ui-corner-all").addClass("ui-corner-top"); 
     }, 
     close: function() { 
      $(this).removeClass("ui-corner-top").addClass("ui-corner-all"); 
     } 
    }); 
}); 
</script> 

我的服務器端腳本使用下面的代碼:

echo $_GET['jsonp_callback'] . '(' . $data . ');'; 

還是要謝謝你

回答

1

使用此行

url: "http://ws.geonames.org/searchJSON?jsonp_callback=?", 

和數據類型也

dataType: 'jsonp', 

代替

url: "http://ws.geonames.org/searchJSON", 
+0

我的錯抄錯碼我用這個網址有我的腳本位於該得到的數據,並使用回聲$ _GET [「jsonp_callback」]。 '('。$ data。');';我使用這個網址http://localhost/jQuery/development-bundle/demos/autocomplete/search3.php – 2012-04-16 10:21:24

+0

我改變了網址爲:http://localhost/jQuery/development-bundle/demos/autocomplete/search3.php ?jsonp_callback =? 現在是我的迴應:?([{「id」:「Jimmy」,「label」:「Jimmy」,「value」:「Jimmy」}]); 但仍下拉爲空 – 2012-04-16 10:24:56

+1

檢查我更新的答案 – 2012-04-16 10:28:51