首页 > 解决方案 > getHistory 函数未定义

问题描述

我有以下代码:

<button type="button" onclick="getHistory()">Refresh</button>
</p>
<br>
<p id="textList">List of texts:</p>
<br>
<p id="textHistory"></p>
<br>
<br>

</div>

<script>

// Gets a list of phone numbers
function getHistory(){
    var success = false;
    // Assemble and send the URL to the Twilio API handler
    //alert(message); // debugging
    var url = 'https://tools.cyntrx.net/GetHistory'
    //var url = 'http://localhost:8000/GetHistory
        
    let response = await fetch(url);
    if (response.ok) { // If HTTP-status is 200-299
      // Get the response body
      let data = await response();
      document.getElementById("textHistory").innerHTML = data;
    } else {
        alert("HTTP-Error: " + response.status);
    }
    if (success) {
        alert("Message sent! (" + displayMsg + ")"); // Alert user of success
    }
}


</script>

当我尝试运行它时,页面会加载,但是当我单击按钮时,我得到 ReferenceError: getHistory is not defined。

我不确定我错过了什么。

标签: javascripthtml

解决方案


您使用await而不用async关键字声明函数。所以你的功能将是:

async function getHistory(){
    //Your codes go here
    let response = await fetch(url);
    //Other codes
}

推荐阅读