javascript - Javascript 变量 - AJAX - 发送到服务器而不等待答案
问题描述
我的点击计数器不向服务器发送变量。
我试图找到一些如何创建的示例,但无论我尝试了什么,都没有尝试将数据发送到服务器。看起来 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的主要原因。
推荐阅读
- mongodb - 从 mongoDB 迁移到 clickhouse 中的嵌套数据结构
- java - 在java中生成-1到1之间的随机数
- android - AWS AppSync - Angular 混合移动应用程序
- aws-lambda - 为什么这个简单的 Lambda 函数的日志中没有显示输出
- amazon-web-services - aws sso 和 EKS rbac 访问被拒绝
- python-3.x - 如何将数据框的每一行和每一列与字符串匹配 - Python
- python - 缺少模块源
- php - 为什么 ASC 的 WP 查询订单不起作用?
- javascript - 下载多层画布图像js的最佳方法
- d3.js - 为什么 d3.select() 在 vuetify+nuxt 环境中不起作用?