[英]How to send a POST request using an html button
I want to send a POST request to the URL, for example, I want to create a database in influxdb by pressing this button.我想向URL发送一个POST请求,例如我想通过按下这个按钮在influxdb中创建一个数据库。 So far I tried this but cannot make it work到目前为止,我尝试了这个但无法使其工作
<button id="post-btn">Post</button>
<script>
const button = document.getElementById('post-btn');
button.addEventListener('click', async _ => {
try {
const response = await fetch('http://00.00.000.000:2000/query', {
method: 'post',
body: {
"q=create database telecom"
}
});
console.log('Completed!', response);
} catch(err) {
console.error(Error: ${err});
}
});
</script>
The error it gives me is "Failed to fetch"它给我的错误是“无法获取”
You have to deal with Cross-Origin Resource Sharing
, so you need to allow your Grafana origin on the InfluxDB server side.您必须处理Cross-Origin Resource Sharing
,因此您需要在 InfluxDB 服务器端允许您的 Grafana 来源。
Anyway it looks very unsafe.无论如何,它看起来非常不安全。 If you can call create database telecom
, then also other Grafana user can requests own queries, eg drop database telecom
.如果你可以调用create database telecom
tele ,那么其他 Grafana 用户也可以请求自己的查询,例如drop database telecom
tele 。 Grafana should have read only access and InfluxDB should be managed outside of Grafana. Grafana 应该具有只读访问权限,并且 InfluxDB 应该在 Grafana 之外进行管理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.