[英]empty json from google maps api url fetch
我正在使用谷歌地圖 API 從坐標中獲取城市名稱。 當我獲取 API url 時,我得到一個空的 json
unction callWCoords(position) {
let latitude = position.coords.latitude;
let longitude =position.coords.longitude;
let call1;
let API1="https://maps.googleapis.com/maps/api/geocode/json?latlng=+"+latitude+","+longitude+"&key=AIzaSyAfH3Ypu0Al8HpNRXhOPEzGLeNbkxOlsoI"
fetch(API1).then(
response=>{alert(JSON.stringify(response));
}).then( data=>{
})
//getPlaceC(call1);
}
如果我在 second.then 塊中嘗試JSON.stringfy(data)
, JSON.stringfy(response)
返回"{}"
。 由於在官方谷歌 json 中,我應該得到一個名為results
的數組,我試圖用data
替換results
,但它沒有用。
function callWCoords(position) {
let latitude = position.coords.latitude;
let longitude =position.coords.longitude;
let API1="https://maps.googleapis.com/maps/api/geocode/json?latlng=+"+latitude+","+longitude+"&key=AIzaSyAfH3Ypu0Al8HpNRXhOPEzGLeNbkxOlsoI"
fetch(API1).then(res => res.json()).then(data => console.log(data))
}
您必須在獲取返回時調用 res.json() function 以獲取數據。 此外,您不會在警報后返回數據,這基本上是從下一個.then
中刪除數據
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.