[英]Stripes and Ajax- How do i send data to a server and refresh a table with data using AJAX
[英]Javascript variable - AJAX- send to server without waitting for answer
我的点击计数器不向服务器发送变量。
我试图找到一些如何创建的示例,但无论我尝试了什么,都没有尝试将数据发送到服务器。看起来 AJAX 应该是最好的选择,但我做错了什么。 我也试过 window.location.href javascript,但这会将我重定向到其他页面,我想继续当前的 srcipt。
法庭1.js:
var button = document.getElementById("clickme"),
count = 0;
button.onclick = function() {
count += 1;
button.innerHTML = "result: " + count;
$.ajax({
type: 'POST',
url: 'count.php',
data:+count,
});
};
索引.html
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
body {
margin: 0;
padding: 0;
font-family: "Lato", sans-serif;
font-size: 20pt;
font-weight: normal;
background: lightblue; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(
-90deg,
lightblue,
black
); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(
-90deg,
lightblue,
black
); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(
-90deg,
lightblue,
black
); /* For Firefox 3.6 to 15 */
background: linear-gradient(-90deg, lightblue, black); /* Standard syntax */
}
.main {
margin: 100px auto;
text-align: center;
}
button {
padding: 20px;
background: transparent;
text-shadow: 1px 1px 1px #202020;
font-family: "Lato", sans-serif;
font-size: 18pt;
border: 1px solid lightblue;
color: lightblue;
}
</style>
</head>
<body>
<div class="main">
<h3>court 1</h3>
<button id="clickme">click here</button>
</div>
<script src="court1.js"></script>
</body>
</html>
计数器工作正常..从 0 刷新计数后。但我没有看到任何将变量发送到服务器的尝试。 chrome 中的调试模式不显示任何活动。
每次触摸(单击)后,我都需要将新值从变量 $count 发送到服务器。 柜台必须继续工作而不受任何打扰。 我们说的是每秒点击几下。
我想向 php 脚本发送新值,或者我也愿意通过 UDP 数据包向服务器发送新值,并通过 syslog-ng 在服务器上收集数据。
优先级是客户端的最小资源负载。 谢谢您的帮助
主要问题是HTML代码中没有加载jquery:
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
这是 ajax 未激活的主要原因。
正确的 ajax 部分发送变量为 ULR/court1.php?“变量”
$.ajax({
type: 'POST',
url: 'court1.php?' + count,
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.