2012-02-08 50 views
0

我有我的網頁上下面的jQuery的片段:Google Maps API爲什麼會返回405錯誤?

$(".lookup").click(function() { 
      var addr =  document.aspnetForm.ctl00_ContentPlaceHolder1_txtEditLocationAddr1.value + ",+" + document.aspnetForm.ctl00_ContentPlaceHolder1_txtEditLocationCity.value + ",+" + document.aspnetForm.ctl00_ContentPlaceHolder1_txtEditLocationState.value; 
      $.ajax({ 
       url: "http://maps.googleapis.com/maps/api/geocode/json?address="+escape(addr)+"&sensor=false", 
       success: function (data) { 
        var mydata = JSON.parse(data); 
        document.aspnetForm.ctl00_ContentPlaceHolder1_txtEditLocationLat.value = mydata.results[0].geometry.location.lat; 
        document.aspnetForm.ctl00_ContentPlaceHolder1_txtEditLocationLong.value = mydata.results[0].geometry.location.lng; 
       } 
      }); 
     }); 

可以看出,這需要地址,市州和郵編的當前形式值,並將其傳遞給谷歌API,然後解析JSON響應並在同一表單中填充經緯度字段。這段代碼在我的本地機器上正常工作。但是當我把它放在我的生產服務器上時,我發現返回了一個405錯誤。 Google Googling 405錯誤後,我想可能是我的服務器正在強制POST,Google需要一個GET,但後來我用Firebug檢查了這些頭文件,並且它們確實以GET方式發送。

什麼會導致Google返回405?解碼的網址我上面用的就是:

http://maps.googleapis.com/maps/api/geocode/json?address=1630%20E.%20Lamar%20Blvd%2C+Arlington%2C+TX&sensor=false

這在地址欄正常工作爲好。

感謝您的任何建議。

+0

重複? http://stackoverflow.com/questions/1783288/jquery-on-google-json-fails-with-error-405 – Kath 2012-02-08 21:10:22

回答

2

無法通過AJAX訪問Geocoding API。請使用Google Maps API V3中的Geocoder

這提供了對Google地理編碼器的異步訪問。

+0

好的。我可以輕鬆地嘗試。爲什麼它會在測試模式下在本地機器上運行?它仍然是阿賈克斯?奇怪... – 2012-02-09 00:29:50

+0

這看起來很奇怪,你是對的:-) – 2012-02-09 03:57:39

相關問題