我从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)
,但这给了我一个错误
位置1的JSON中出现意外的令牌o
答案 0 :(得分:1)
你得到了这个
Unexpected token o in JSON at position 1
因为您的数据已经是一个对象。无需解析它。 javascript解释器已经为您解析了它。
所以你应该尝试这样做JSON.parse(JSON.stringify(data));