JavaScript 代码调试:使用 Fetch API 获取 JSON 数据并渲染
<form id='result2'></form>
<script>
fetch('/getAudio', { method: 'POST' })
.then(response => response.json())
.then(data => {
document.getElementById('result2').innerHTML = data['Rescoring_result'];
})
.catch(error => console.error(error));
</script>
<p>以下是已经调试过的代码:</p>
<pre><code class="language-html"><form id='result2'></form>
<script>
fetch('/getAudio', { method: 'POST' })
.then(response => response.json())
.then(data => {
document.getElementById('result2').innerHTML = data['Rescoring_result'];
})
.catch(error => console.error(error));
</script>
</code></pre>
<p>主要的修改点有:</p>
<ol>
<li>取消了多余的反斜杠</li>
<li>去掉了多余的 <code>JSON.parse()</code>,因为 <code>response.json()</code> 已经返回解析后的 JSON 对象</li>
<li>直接使用 <code>data['Rescoring_result']</code>,不再需要将其赋值给一个中间变量</li>
</ol>
原文地址: https://www.cveoy.top/t/topic/lAQU 著作权归作者所有。请勿转载和采集!