请问可以出一个html调用api运行脚本的demo吗
以下是我的代码,但是貌似api运行不了。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hamibot 脚本控制台</title>
<style>
body {
font-family: "Microsoft YaHei", Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}
.container {
text-align: center;
background: rgba(255, 255, 255, 0.95);
padding: 30px 40px;
border-radius: 15px;
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
backdrop-filter: blur(10px);
max-width: 500px;
width: 90%;
}
h1 {
color: #2c3e50;
margin-bottom: 25px;
font-size: 1.8em;
}
button {
padding: 12px 30px;
font-size: 16px;
color: white;
background: #3498db;
border: none;
border-radius: 25px;
cursor: pointer;
transition: all 0.3s ease;
box-shadow: 0 4px 15px rgba(52, 152, 219, 0.3);
}
button:hover {
background: #2980b9;
transform: translateY(-2px);
}
button:active {
transform: translateY(0);
}
.status {
margin-top: 25px;
padding: 15px;
border-radius: 8px;
font-size: 14px;
display: none;
}
.success {
background: #e8f5e9;
color: #2ecc71;
border: 1px solid #2ecc71;
}
.error {
background: #ffebee;
color: #e74c3c;
border: 1px solid #e74c3c;
}
.device-info {
margin: 15px 0;
padding: 10px;
background: #f8f9fa;
border-radius: 8px;
color: #7f8c8d;
}
</style>
</head>
<body>
<div class="container">
<h1>Mi8 设备脚本控制台</h1>
<div class="device-info">
设备ID:66557b2ab5e40d937bc64449<br>
最后更新:2024年5月28日
</div>
<button id="runScriptButton">启动自动化脚本</button>
<div class="status" id="statusMessage"></div>
</div>
<script>
document.getElementById('runScriptButton').addEventListener('click', async function() {
const scriptId = '';//脚本id
const authToken = ''; // 令牌
const deviceId = '';//设备id
const deviceName = '';//name
const statusDiv = document.getElementById('statusMessage');
statusDiv.style.display = 'none';
this.disabled = true;
this.textContent = '执行中...';
try {
const response = await fetch(`https://api.hamibot.com/v1/scripts/${scriptId}/run`, {
method: 'POST',
headers: {
'Authorization': authToken,
'Content-Type': 'application/json'
},
body: JSON.stringify({
devices: [{ _id: deviceId, name: deviceName }]
})
});
if (response.ok) {
statusDiv.className = 'status success';
statusDiv.innerHTML = '✅ 脚本已成功启动!<br><small>设备响应时间:' + new Date().toLocaleTimeString() + '</small>';
} else {
const errorText = await response.text();
throw new Error(`HTTP 错误码:${response.status},错误信息:${errorText}`);
}
} catch (error) {
statusDiv.className = 'status error';
statusDiv.textContent = '❌ 执行失败:' + error.message;
console.error('错误详情:', error); // 打印详细错误信息到控制台
} finally {
statusDiv.style.display = 'block';
this.disabled = false;
this.textContent = '启动自动化脚本';
}
});
</script>
</body>
</html>
回答
看下有没有报错信息,运行脚本正常是返回 204