我想从我的Mysql数据库中获取数据而不刷新页面。我在technotip.com上找到了编码。 fetch.php文件很好并且执行我想要的数据并且看起来像technotip示例中的那样。但是,当我加载index.html文件时,它不会加载任何东西。请帮帮我。
Php文件
<?php
$fXg = 0;
$fYg = 0;
$fZg = 0;
$alpha = 0.5;
include '../connection.php';
$data = mysql_query("SELECT * FROM Sensor1 WHERE id_wasp = 'Sensor1' and sensor='ACC'")
or die(mysql_error());
$result = array();
while($info = mysql_fetch_array( $data ))
{
$acc = explode(";",$info['value']);
$X = $acc[0];
$Y = $acc[1];
$Z = $acc[2];
//low pas filter
$fXg = $X * $alpha + ($fXg * (1.0 - $alpha));
$fYg = $Y * $alpha + ($fYg * (1.0 - $alpha));
$fZg = $Z * $alpha + ($fZg * (1.0 - $alpha));
//Roll & Pitch Equations
$roll = (atan2(-$fYg, $fZg)*180.0)/M_PI;
$pitch = (atan2($fXg, sqrt($fYg*$fYg + $fZg*$fZg))*180.0)/M_PI;
$frameno = $info['frame_number'];
array_push($result, array('pitch' => $pitch,
'roll' => $roll));
//echo "$pitch<br />\n";
//echo "$roll<br />\n";
//echo "$frameno<br />\n";
}
echo json_encode(array("result" => $result));
//print_r($result);
?>
my_script.js
$(document).ready( function() {
done();
});
function done() {
setTimeout( function() {
updates();
done();
}, 200);
}
function updates() {
$.getJSON("fetch.php", function(data) {
$("ul").empty();
$.each(data.result, function(){
$("ul").append("<li>Pitch: "+this['pitch']+"</li>
<li>Roll: "+this['roll']+"</li>
<br />");
});
});
}
的index.html
<html>
<head>
<title>Fetch/Extract Data From Database: jQuery + JSON + PHP+ AJAX</title>
</head>
<body>
<ul></ul>
<script type="text/javascript" src="jquery-2.1.0.js"></script>
<script type="text/javascript" src="my_script.js"></script>
</body>
</html>
答案 0 :(得分:1)
我刚刚在本地重新创建了您的问题,并发现了它的奇怪问题。以下行出现语法错误:
$("ul").append("<li>Pitch: "+this['pitch']+"</li>
jQuery对换行符不满意,如果你将所有东西都放在一行中,消除了标签和换行符,它的效果非常好。
$("ul").append("<li>Pitch: "+this['pitch']+"</li><li>Roll: "+this['roll']+"</li><br />");