我不确定哪个部分导致页面响应没有。这个页面是我的lubuntu机器的主机。但是,当我从另一台局域网计算机访问此页面时,选择startDate为“2012-12-16”并将endDate选为“2013-12-16”,该页面将在Chrome浏览器中冻结。什么时候冻结?
当我拨打$.get('duty.php'...
时,警报甚至无法弹出。
当我直接去
/attendence/duty.php?mode=9&startDate=2012-12-06&endDate=2013-12-06&christened=All
回报是
[]
* 编辑2 *
找到原因,结果证明是另一个“愚蠢”的问题。问题是由$.get()
引起的。正如您所看到的,php的输出是一个空数组[]
,这没关系。然而$.get()
函数没有看到它是一个有效的响应,只是等到浏览器超时。(使用jQuery 1.7.2)解决方法是返回json,其中包含{'no','result'}
等内容。
Apache2 access.log
192.168.1.7 - - [06 / Dec / 2012:21:02:48 +0800]“GET /attendence/duty.php?mode=9&startDate=2012-12-06&endDate=2013-12- 06& christened =所有HTTP / 1.1“500 411” - “”Mozilla / 5.0(X11; Linux i686)AppleWebKit / 535.19(KHTML,如Gecko)Ubuntu / 11.10 Chromium / 18.0.1025.168 Chrome / 18.0.1025.168 Safari / 535.19“
$.get('duty.php',
{'mode':9,
'startDate':$('input[name=startDate]').val(),
'endDate':$('input[name=endDate]').val(),
'christened':$('input[name=christened]:checked').val()},
function(data, textStatus, jqXHR){
alert('result reach back');
if (typeof data === 'undefined') {
return;
}
numWeek = getDiffWeek();
tableHtml='Num Of week:'+numWeek+'<table border=1><tr>';
tableHtml+='<td>Barcode</td><td>name</td><td>attendence</td><td>frequency</td>';
tableHtml+=' </tr></table>';
$('#attendenceRate').html(tableHtml);
for(name in data){
attendenceRate = Math.round(data[name]['times']/numWeek*100);
memberIcon ='';
$('#attendenceRate table').append('<tr><td>'+data[name]['barcode']+'</td><td>'+name+'</td><td>'+attendenceRate+'%</td><td>'+data[name]['times']+'</td></tr>');
}
}
,'json'
);
编辑只是一个错字,应该 duty.php 而不是Duty.php
include ("lock.php");
if($_GET){
if ($_GET['mode']==9){//calculate overall christian attendence
$startDate = $_GET['startDate'];
$endDate = $_GET['endDate'];
$christened=$_GET['christened'];
if($christened=='All'){
$christenedClause='';
}else{
$christenedClause= ' AND record.christened = '.$christened;
}
$sql = <<<EOD
SELECT name,barcode, COUNT( * ) AS times ,christened,gender
FROM (
SELECT name,attendence.barcode as barcode, DATE, TIME,christened,gender
FROM attendence, record
WHERE attendence.barcode = record.barcode
AND DATE
BETWEEN "$startDate"
AND "$endDate"
$christenedClause
GROUP BY name, DATE
)A
GROUP BY name
EOD;
$result = $link->query($sql);
$data = array();
$i=0;
if($result->num_rows>0){
while ($result2 = $result->fetch_assoc()){
$data[$result2['name']]['times'] = $result2['times'];
$data[$result2['name']]['barcode'] = $result2['barcode'];
$data[$result2['name']]['gender'] = $result2['gender'];
$data[$result2['name']]['christened'] = $result2['christened'];
$i++;
}
}
echo json_encode($data);
}
}
答案 0 :(得分:2)
您的网页引发了服务器错误。访问日志中的500
(All HTTP/1.1" 500 411
)表示无法加载页面。您需要在PHP页面中修复一些内容。
首先,我不会使用.get()
命令而是使用.ajax()
命令。 .ajax()
命令包括在失败时中止(.fail()
)并在设定的时间(timeout:
)后停止尝试的方法。
$.ajax({
url: url,
type: "GET",
timeout: 1000,
data: { 'mode':9,
'startDate':$('input[name=startDate]').val(),
'endDate':$('input[name=endDate]').val(),
'christened':$('input[name=christened]:checked').val()
}
}).done(function(data) {
alert('success!');
// ...
}).fail(function(jqXHR, textStatus, errorThrown) {
alert('fail :(');
// ...
});
其次,您的SQL可以清理。正如@Shivan指出的那样,查询中的许多单词(例如:date
,time
,record
)都是关键字:用作函数或数据类型的单词。如果您尝试使用其中一个单词作为列名,MySQL会感到困惑,因此您需要使用反引号(`
)字符来转义它。
我还把日期放在单引号('
)
SELECT
`name`, `barcode` , COUNT( * ) AS `times`, `christened`, `gender`
FROM (
SELECT
`name`, attendence.barcode as `barcode`, `DATE`, `TIME`, `christened`, `gender`
FROM
`attendence`, `record`
WHERE
`attendence`.`barcode` = `record`.`barcode`
AND `DATE`
BETWEEN '$startDate'
AND '$endDate'
$christenedClause
GROUP BY `name`, `DATE`
)A
GROUP BY name
我认为这不会解决您的所有问题,因此您应该在PHP页面上启用错误报告。将此代码放在duty.php
:
ini_set('display_errors',1);
error_reporting(E_ALL);
如果这样可行,那么PHP将显示错误消息而不是失败。
答案 1 :(得分:1)
在$ data数组中添加任何内容,以便它不会返回空的json。 在duty.php结束时添加
...
if( count($data)==0){
$data['content']=['none'];
}
echo json_encode($data);