javascript - 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。
我不确定我错过了什么。
解决方案
您使用await
而不用async
关键字声明函数。所以你的功能将是:
async function getHistory(){
//Your codes go here
let response = await fetch(url);
//Other codes
}
推荐阅读
- ionic-framework - 如何在离子选项标签中添加新行?
- perl - 如何使用 perl 获取最近创建的目录?
- python - 使用“as”导入时,Python 模块没有属性错误
- objective-c - 如何在 macOS 上取消转义反斜杠?
- c# - RabbitMQ 多个消费者在同一个进程中
- php - php mysql查询最后加引号
- performance - Cognos 中的条件格式和性能
- angular - 用于路由的 Angular 7 写作测试
- reporting-services - 添加“maxRequestLenght”参数会导致 SSRS 2016 中的内部服务器错误
- java - 当页面左右滚动时,如何在 Selenium 中捕获整个屏幕