【发布时间】:2018-04-20 09:45:34
【问题描述】:
我正在从 Google Place Search API 获取一些数据,但我不知道为什么我无法访问其中的数据。下面是我的代码。
getData = (keyword, location, country) => {
let dataURI = `${URI}${keyword}+${location}+${country}${API}`;
var proxyUrl = 'https://cors-anywhere.herokuapp.com/',
targetUrl = dataURI
fetch(proxyUrl + targetUrl)
.then((res) => res.json())
.then((data) => {
console.log(data); //reurns JSON data
console.log(data.results); //Returns Undefined
})
.catch((e)=> console.log(`Error! ${e.message}`));
}
当我写 console.log(data) 这将返回以下内容
JSON data
但是当我执行console.log(data.results) 时,这将返回未定义。
我也试过JSON.parse(data),但这给了我一个错误
JSON 中位置 1 的意外标记 o
【问题讨论】:
-
你需要解析类似
let json = JSON.parse(data); console.log(json.results);的数据 -
需要像这样解析响应。
JSON.parse(data)之前访问data.results -
这给了我一个错误
Unexpected token o in JSON at position 1
标签: javascript json reactjs google-maps-api-3