javascript - 来自谷歌地图 api url fetch 的空 json
问题描述
我正在使用谷歌地图 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);
}
JSON.stringfy(response)
"{}"
如果我JSON.stringfy(data)
在第二个 .then 块中尝试,也会返回。因为在官方的 google json 中,我应该得到一个名为results
我试图替换的数组results
,data
但它没有用。
解决方案
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))
}
您必须在 fetch 返回时调用 res.json() 函数来获取数据。此外,您不会在警报后返回数据,这基本上是从下一个删除数据.then
推荐阅读
- c# - 为什么在 nodejs 中循环比在 c# 中更快(dotnet 6)
- sql-server - XQuery sql 仅在存在时选择节点
- c# - RangeColumn:将两个 y 值之间的差异显示为标签
- javascript - 为什么 Yes 是大小写敏感的?
- javascript - node.js - 如何将状态码添加到 Express
- python - 如何验证完美的正弦波?
- python - 将 pandas 数据帧转换为 JSON,字符串分隔
- sql - 有没有更简单的方法来连接具有相同条件的多个表/视图?
- python - 检查行级别是否仅填充一列的简洁方法
- html - 为什么我可以将标题放在中心。但不能把标题孩子放在中心?