如何以某种方式对post返回的数据求和
How to sum the data returned from post, one way or another
下面的代码基本上查询一个PHP文件,该文件查询数据库中的cpu_names(在我的测试用例中总共有4个)和每个cpu_names的相关负载。然后我需要取这四个CPU时间中的每一个,将它们加在一起,然后除以CPU的总数。这就得到了总CPU负载。因此,对于测试用例,这段代码总共升级了5个CPU仪表,其中一个是total。唯一的问题是,无论我用哪种方法,我都无法对这些帖子求和!谢谢。
setInterval(function() {
fill_sum=0.0;
$("canvas[name='gauge']").each(function(){
var cpu_name=this.innerHTML;
var ctx = this.getContext('2d');
var padding = this.style.padding.replace(/[^0-9]+/, '');
var w=this.width
var h=this.height;
//var fill_percent=.02;
$.post("BH_Responder.php",
{cpu_use: true, cpu_name: cpu_name},
function(data, status){
fill_percent = data;
ctx.clearRect(0, 0, w, h);
DrawGauge(ctx, w, h, fill_percent);
}
).done(function() {
fill_sum = fill_sum+parseFloat(fill_percent);
});
});
alert(fill_sum);
total_percent = fill_sum/<?php echo $num_rows_cpu; ?>;
$("canvas[name='master_gauge']").each(function(){
var ctx = this.getContext('2d');
var padding = this.style.padding.replace(/[^0-9]+/, '');
var w=this.width
var h=this.height;
//var fill_percent=.02;
ctx.clearRect(0, 0, w, h);
DrawGauge(ctx, w, h, total_percent);
});
}, 100);
PHP脚本部分:
}elseif (isset($_POST['cpu_use']) == true && empty($_POST['cpu_use'])==false && $_POST['cpu_use']==true){
$cpu_name = $_POST['cpu_name'];
$sql= mysql_query("SELECT used FROM tbl_cpu_use WHERE cpu_name='$cpu_name' ORDER BY id DESC LIMIT 1;");
echo (mysql_num_rows($sql) != 0) ? mysql_result($sql,0,'used') : NULL;
}
对于AJAX、回调和所有这些的一个很好的解释
看起来你几乎有。done(),但你需要把所有这些东西:
total_percent = fill_sum/<?php echo $num_rows_cpu; ?>;
$("canvas[name='master_gauge']").each(function(){
...
DrawGauge(ctx, w, h, total_percent);
});
中的.done()回调,但只调用它的最后一个ajax响应。要么跟踪你在ajax请求之前设置的计数器,要么跟踪你在PHP端会发出多少请求。
类似:
var totalCpus = <?php echo $numCpus; // assuming you're using PHP to echo out the gauges in
// the first place. otherwise use ?>
var totalCpus = $("canvas[name='gauge']").length; // use this.
// assuming of course, that the # of gauges is the same throughout each interval
setInterval(function() {
fill_sum=0.0;
$("canvas[name='gauge']").each(function(){
var numDoneRequests = 0; // this one keeps track of *dun dun dunnn* the number of requests that have completed
var cpu_name=this.innerHTML;
...
).done(function() {
fill_sum = fill_sum+parseFloat(fill_percent);
numDoneRequests++;
if(numDoneRequests === totalCpus) {
total_percent = fill_sum/<?php echo $num_rows_cpu; ?>;
$("canvas[name='master_gauge']").each(function(){
var ctx = this.getContext('2d');
...
DrawGauge(ctx, w, h, total_percent);
});
...
alert(fill_sum);
}, 100);
还有恐惧@alfasin的朋友,小Bobby Tables。当你做
$sql= mysql_query("SELECT used FROM tbl_cpu_use WHERE cpu_name='$cpu_name' ORDER BY id DESC LIMIT 1;");
^^^^^^^^^
你将完全未经检查的用户输入插入到一个查询中,这个查询将进入你的数据库,并将你自己打开SQL注入。我意识到这可能是一个人为的例子,如果是的话,我很抱歉指出了一些你已经知道的事情。
(另外,请查看"为什么我不应该't在PHP中使用mysql_*函数?",因为你不应该使用mysql_库连接到数据库和一些漂亮的替代方案)
相关文章:
- 当method=post时,jQueryAjax返回404
- 数据未从ajax POST返回
- post()向服务器动态生成的数据返回空响应
- DreamFactory REST API POST休息/用户/会话请求总是在IE9中返回错误
- PHP/Ajax/jquery/JON-在Ajax Post之后将echo文本中的一部分作为变量返回
- 为什么我的$http.post返回一个400错误
- AngularJS$http.post调用返回502(坏网关)错误
- 为什么Hapi.js POST处理程序返回空负载
- 如何将多个onError函数分配给promise(由angular's$http.post返回)
- jQuery's$.post只返回一个空白数据
- 已将POST更改为GET.现在Ajax正在返回多个帖子
- Ajax POST返回成功,但没有;t更新JSON文件
- 从返回的数据 ajax post 创建一个变量
- jQuery通过父函数返回$.post回调
- Javascript确认返回post值
- ajax正在加载页面以返回POST请求
- AJAX找不到php文件.它返回"POST 405(方法不允许)"
- 当使用$http发送字符串请求时,请求体作为对象返回.Post (angular)和bodyparser.text()
- 返回post数据角
- 包裹角度$resource请求,但不返回POST数据