我这里有语法错误吗?
<script type="text/javascript">
async function showWidget(physicianId, locationId, format)
{
var response = await fetch('https://reviews.rater8.com/webwidget/api/ratingsummary/' + physicianId + '?format=' + format);
var snippet = await response.text();
document.getElementById(locationId).innerHTML = snippet;
}
</script>
此功能有效,您可以在以下位置查看页面:
https://reviews.rater8.com/webwidget/sample.html
但是,Google 不会通过该功能看到我动态插入页面的内容。我使用了 Google Search Console 来查看原因,并发现 Google 标记该页面有两个错误:
(即关键字“async”之后的关键字“function”。)
自然地,如果函数定义没有解析,那么函数就不会被定义。我可以通过删除关键字 async 来消除解析错误,但是该函数无法正确执行!
以下是如何在不使用 javascript await 机制的情况下完成工作。
<script type="text/javascript">
function showWidget(physicianId, locationId, format)
{
fetch('https://reviews.rater8.com/webwidget/api/ratingsummary/' + physicianId + '?format=' + format)
.then(function (response) {
response.text()
.then(function (snippet) {
document.getElementById(locationId).innerHTML = snippet;
})
})
}
</script>
Google 能够看到通过这种方法生成的内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句