首页 > 解决方案 > 如何从 json 格式的 promise 对象中获取数据

问题描述

我正在尝试使用 fetch 计算与 google api 的距离,现在我将数据转换为 promise 对象中的 json,json fromat 是我的代码,也是我现在得到的截图

我尝试到目前为止,但每次都得到未定义的错误。

fetch(proxyurl + re)
            .then(function(response) {

                console.log (response.json());
            });

我想获取源和目的地时间和距离的数据

这是截图https://i.stack.imgur.com/Dz51R.png

标签: javascriptpromisegoogle-api

解决方案


尝试像这样更新您的 fetch 方法:

fetch('https://api-endpoint.com')
    .then(response => response.json())
    .then(data => {
       console.log(data);
    });

带有 github 用户端点的演示:https ://codesandbox.io/s/prod-bush-wqn70


推荐阅读