我正在从 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)
时,会返回 Undefined。
我也尝试过 JSON.parse(data)
但这给了我一个错误
Unexpected token o in JSON at position 1
最佳答案
你明白了
Unexpected token o in JSON at position 1
因为你的数据已经是一个对象了。无需解析它。 javascript解释器已经帮你解析了。
所以你应该尝试这样做 JSON.parse(JSON.stringify(data));
关于javascript - 无法从 Google Place Search API 返回的 JSON 中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49938758/