2013-10-29 90 views
1

所以返回的值,我想不通,爲什麼這是行不通的:無法檢索JSON由MapQuest的API

$.getJSON('http://www.mapquestapi.com/geocoding/v1/reverse?key=KEY&callback=renderReverse&json&location=' + lat + ',' + longi, function(geo) { 
    $('#location').html(geo.response.results[0].locations[0].adminArea4); 
}); 

返回的JSON是:

{ 
    "results": [ 
     { 
      "locations": [ 
       { 
        "latLng": { 
         "lng": -1, 
         "lat": 51 
        }, 
        "adminArea4": "Hamp", 
        "adminArea5Type": "City", 
        "adminArea4Type": "County", 
        "adminArea5": "Ba and De", 
        "street": "Teal Crescent", 
        "adminArea1": "GB", 
        "adminArea3": "England", 
        "type": "s", 
        "displayLatLng": { 
         "lng": -1.145731, 
         "lat": 51.240441 
        }, 
        "linkId": 0, 
        "postalCode": "RG3", 
        "sideOfStreet": "N", 
        "dragPoint": false, 
        "adminArea1Type": "Country", 
        "geocodeQuality": "ADDRESS", 
        "geocodeQualityCode": "L1AAA", 
        "mapUrl": "http://www.mapquestapi.com/staticmap/v4/getmap?key=Fmjtd|luubnu0yn5,7g=o5-9072ur&type=map&size=225,160&pois=purple-1,51.2401563,-1.1463337,0,0|&center=51,-17&zoom=15&rand=-859825484", 
        "adminArea3Type": "State" 
       } 
      ], 
      "providedLocation": { 
       "latLng": { 
        "lng": -1.146334, 
        "lat": 51.240156 
       } 
      } 
     } 
    ], 
    "options": { 
     "ignoreLatLngInput": false, 
     "maxResults": -1, 
     "thumbMaps": true 
    }, 
    "info": { 
     "copyright": { 
      "text": "© 2013 MapQuest, Inc.", 
      "imageUrl": "http://api.mqcdn.com/res/mqlogo.gif", 
      "imageAltText": "© 2013 MapQuest, Inc." 
     }, 
     "statuscode": 0, 
     "messages": [] 
    } 
} 

我猜測這是因爲我得到geo.response.results位錯了!

幫助將不勝感激。

+0

請更改您的問題標題! –

+2

你確定'$('#location')。html('甚至在執行嗎?我希望這會失敗,因爲你告訴服務把它作爲一個特定的回調用'&callback = renderReverse'返回,除非服務忽略它並返回CORS頭文件 –

+0

@KevinB:我改變了&callback = renderReverse爲&callback =? 現在正在工作,我從mapquests的api文檔中抓取了這個url,並忘記它是用於JavaScript的!非常感謝! – GeorgeTaylor

回答

3

返回的JSON中沒有response鍵。試試這個:

$.getJSON('http://www.mapquestapi.com/geocoding/v1/reverse?key=KEY&callback=renderReverse&json&location=' + lat + ',' + longi, function(geo) { 
    $('#location').html(geo.results[0].locations[0].adminArea4); 
}); 
+1

@ComFreek感謝您的編輯。始終最好隱藏您的API密鑰OP。 –

+1

如果你運行它,你的答案是正確的,但永遠不會被解僱。我調試,這是因爲在JSON請求失敗語法錯誤:parsererror,SyntaxError:JSON.parse:意外字符 –

+0

@ComFreek:乾杯,你說得對! (感謝API密鑰隱藏:)) – GeorgeTaylor

1

雖然上面的回答是正確的,但你的服務返回的json似乎也有問題。它包含無效字符,用於處理返回錯誤的請求。

如果你使用JSON的成功和錯誤信息你會看到這個

var lat = -1.146334; 
var longi = 51.240156; 


    $.getJSON('http://www.mapquestapi.com/geocoding/v1/reverse?key=**YOURKEY**&callback=renderReverse&json&location=' + lat + ',' + longi) 
    .done(function(json) { 
    console.log("JSON Data: " + json); 
    }) 
    .fail(function(jqxhr, textStatus, error) { 
    var err = textStatus + ", " + error; 
    console.log("Request Failed: " + err); 
    }); 

的問題是,因爲你是在你的迴應越來越JSON頁。爲了解決這個問題,你需要聲明jsonp,以得到一個正確的json對象。

var lat = -1.146334; 
var longi = 51.240156; 


$.getJSON('http://www.mapquestapi.com/geocoding/v1/reverse?key=**YOURKEY**&callback=renderReverse&json&location=' + lat + ',' + longi, {format: "jsonp"}) 
.done(function(geo) { 

$('#location').html(geo.results[0].locations[0].adminArea4); 
}) 
.fail(function(jqxhr, textStatus, error) { 
var err = textStatus + ", " + error; 
console.log("Request Failed: " + err); 
});