2016-02-14 21 views
-2

下面是代碼有誰能告訴我爲什麼我無法從google apis獲取城市列表?

<script> 
    $.ajax({ 
     url: "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=(cities)&language=pt_BR&key=AIzaSyA8FpY17IMXOcKg7Zwzdv2GdTQLR3OnTTk", 
     type: "GET", 
     dataType: 'jsonp', 
     cache: false, 
     success: function (response) { 
      console.log(response); 
     } 
    }); 
</script> 

後,我執行了以上我總是得到這個錯誤。

maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=(citi…nTTk&callback=jQuery1111044755223696120083_1455461549950&_=1455461549951:2 Uncaught SyntaxError: Unexpected token :

+1

插件該URL(它是一個在地圖的JavaScript-API庫)進入一個新的瀏覽器標籤,你會看到問題。 – Pointy

+0

我已經嘗試使用https。無論如何,我會更具體地改變顯示錯誤的問題。 –

+1

這意味着您無法從瀏覽器訪問該URL。您必須從服務器上的代碼執行此操作。 – Pointy

回答

0

places-webservice(當前)不支持JSONP或XDomainRequests。

要請求自動完成的預測值從客戶端必須使用地庫的Autocomplete-Service

google.maps.event.addDomListener(window,'load',function() { 
 
    var displaySuggestions = function(predictions, status) { 
 
    if (status != google.maps.places.PlacesServiceStatus.OK) { 
 
     alert(status); 
 
     return; 
 
    } 
 

 
    predictions.forEach(function(prediction) { 
 
     var li = document.createElement('li'); 
 
     li.appendChild(document.createTextNode(prediction.description)); 
 
     document.getElementById('results').appendChild(li); 
 
    }); 
 
    }; 
 

 
    var service = new google.maps.places.AutocompleteService(); 
 
    service.getQueryPredictions({ input: 'Vict',types:['(cities)'] }, displaySuggestions); 
 
});
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&language=pt-BR"></script> 
 
<img src="https://developers.google.com/places/documentation/images/powered-by-google-on-white.png"/> 
 
<ul id="results"></ul>

+0

謝謝你的回答。 –

相關問題