<form id='result2'>方法2</form>
<script>
  fetch('http://127.0.0.1:9999/getAudio', { method: 'POST' })
    .then(response => response.text())
    .then(response => JSON.parse(response))
    .then(response => document.getElementById('result2').innerHTML = response['Rescoring_result'])
    .catch(error => console.error(error))
</script>
<p>需要将第二个 then 中的参数 response 改为 response.text(),因为 fetch 返回的是一个 Response 对象,需要调用其中的 text() 方法将其转化为文本格式,然后才能使用 JSON.parse() 方法解析。修改后的代码如下:</p>
<form id='result2'>方法2</form>
<script>
  fetch('http://127.0.0.1:9999/getAudio', { method: 'POST' })
    .then(response => response.text())
    .then(response => JSON.parse(response))
    .then(response => document.getElementById('result2').innerHTML = response['Rescoring_result'])
    .catch(error => console.error(error))
</script>
Fetch API 修改代码获取正确值:使用 response.text() 解析 JSON 数据

原文地址: https://www.cveoy.top/t/topic/lBfr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录