[英]unable to access data in fetch API call
I am trying to fetch data from https://lighthouse-dot-webdotdevsite.appspot.com//lh/newaudit I am getting result in promise but I need a specific thing like performance(score) and SEO all the important things and firstly I did that with help of the following code我正在尝试从https://lighthouse-dot-webdotdevsite.appspot.com//lh/newaudit获取数据我在 promise 中得到结果,但我需要一个特定的东西,比如性能(分数)和 SEO 所有重要的东西,首先我在以下代码的帮助下做到了
<script>
fetch("https://lighthouse-dot-webdotdevsite.appspot.com//lh/newaudit", {
method: "POST",
body: JSON.stringify({
"url": "https://piyushsthr.netlify.app",
"replace": true,
"save": false
}),
headers: {
"Content-Type": "application/json; charset=utf-8"
},
credentials: "same-origin"
}).then(function(response) {
response.status
response.statusText
response.headers
response.url
return console.log(response.text())
}).catch(function(error) {
error.message
})
</script>
now it is returning promise but after doing console.log(text.value.lhrSlim.performance.score()) it is not showing logs can you guys help for this.现在它正在返回 promise 但是在执行 console.log(text.value.lhrSlim.performance.score()) 之后它没有显示日志你们可以帮忙吗?
fetch API returns a json()
Promise
response which you can chain a .then()
method to get the actual values you need. fetch API 返回一个
json()
Promise
响应,您可以链接一个.then()
方法来获取您需要的实际值。
So it should like this所以它应该像这样
fetch("https://lighthouse-dot-webdotdevsite.appspot.com//lh/newaudit", {
method: "POST",
body: JSON.stringify({
"url": "https://piyushsthr.netlify.app",
"replace": true,
"save": false
}),
headers: {
"Content-Type": "application/json; charset=utf-8"
},
credentials: "same-origin"
})
.then(res => res.json()) // this is the line you need
.then(function(data) {
console.log(data)
return data;
}).catch(function(error) {
error.message
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.