Php jQuery Ajax Json响应-检查是否为null
我使用jQueryAjax和JSON从MySQL获取数据。但是,我需要知道响应何时为空。我将一个日期参数传递给我的php,它将返回一些数据或不返回。。。取决于日期 我的Javascript代码(简单版本): 例如,如果我使用了昨天(2015-09-26)的数据,我会得到以下json数据(只是其中的一部分): 现在,例如,如果我选择了一个没有数据的日期,它将返回:Php jQuery Ajax Json响应-检查是否为null,php,jquery,mysql,json,ajax,Php,Jquery,Mysql,Json,Ajax,我使用jQueryAjax和JSON从MySQL获取数据。但是,我需要知道响应何时为空。我将一个日期参数传递给我的php,它将返回一些数据或不返回。。。取决于日期 我的Javascript代码(简单版本): 例如,如果我使用了昨天(2015-09-26)的数据,我会得到以下json数据(只是其中的一部分): 现在,例如,如果我选择了一个没有数据的日期,它将返回: [] 在我下面的javascript代码中,我想在json数组为空的情况下向success函数添加一个if语句。。。比如: fun
[]
在我下面的javascript代码中,我想在json数组为空的情况下向success函数添加一个if语句。。。比如:
function pac_irra(date){
.ajax({
url: 'get_db_data/pac_irra.php?date='+date,
dataType: 'JSON',
type: 'POST',
data: {get_values: true},
beforeSend: function(){
$("#morris-line-chart").append(load_img);
},
success: function(response) {
date = response[0].Timestamp;
$('#a1').append(date);
if ( ***array is empty***) {
('#a1').append('No data');
};
},
});
}
在我的success函数中,我使用json数据创建了一个morris图表。。。我没有把它写进密码里
那么如何检查它是否为空?我已经做了很多尝试:
if (response.length == 0)
还是另一次尝试
if (response[0].Timestamp == "") or if (!response)
什么都不管用。。。我仍然无法检查json数组是否为空
我的php代码:
<?php
error_reporting(0);
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/database/db_connect.php";
include_once($path);
if(isset($_GET['date'])) {
$date = $_GET['date'];
$days = $_GET['days'];
$var = array();
$query = "SELECT CONVERT_TZ(CONCAT(Date,' ',pac.Time), 'UTC', 'Europe/Lisbon' ) as Timestamp, ROUND((pac.Value/6440)*100,2) pac, ROUND((irra.Value/1000)*100,2) irra
FROM AggData pac
LEFT JOIN (SELECT Time, Value
FROM AggData WHERE Date=DATE_ADD('$date',INTERVAL '$days' DAY) and idEquipment=5 and idMeasure=6 ) irra
ON pac.Time=irra.Time
Where pac.Date=DATE_ADD('$date',INTERVAL '$days' DAY) and pac.idEquipment=1 and pac.idMeasure=3
AND (irra.Value>0 or pac.Value>0)
Order BY pac.Time asc
" or die("Error in the consult.." . mysqli_error($link));
$result = $link->query($query);
while($obj = mysqli_fetch_object($result)) {
$var[] = $obj;
}
echo json_encode($var);
}
?>
如果要检查各种情况,请尝试以下操作
if((response && jQuery.isArray(response))?(response.length==0?false:(jQuery.isPlainObject(response[0])?!jQuery.isEmptyObject(response[0]):response[0])):response){
//console.log("adfdsaf");
}
如果它会检查
响应为null或未定义
或响应==[]
或响应==[{}]或响应[“”]
我认为这就是问题所在
success: function(response) {
if((response && jQuery.isArray(response))?(response.length==0?false:(jQuery.isPlainObject(response[0])?!jQuery.isEmptyObject(response[0]):response[0])):response){
('#a1').append('No data');
}else{
date = response[0].Timestamp;
$('#a1').append(date);
}
};
},
希望它能工作您可以使用response.length==0可能重复的var rep=JSON.parse(response);if(rep!=null | | rep==“”)
aldrin27不起作用:(为什么不在后端发送响应之前检查查询结果是否为null?如果为null,则返回一个字符串“未找到结果”以及一个状态错误。我已经尝试了您的代码…但是如果((response&&jQuery.isArray(response))?(response.length==0?false:(jQuery.isPlainObject(response[0])?!jQuery.isEmptyObject(response[0]):response(response){if(response=[[]| | | response=[{}]| | response[“”]{$('#图表_标题')。append('No data!');});你能把你收到的回复发给我吗?用console.log(response)打印出来就行了。可能您得到的是字符串格式为“[]”的响应。如果您发送给我,将有助于gr8解决此问题。感谢找到问题..函数(响应){date=response[0]。时间戳;$('#a1')。追加(日期);If(数组为空)……由于我们没有检查响应是否未定义或为空..并尝试检索时间戳..响应[0]。时间戳..我已更新了答案,请检查它,因为我们正在尝试检索响应[0].Timestamp,在成功块中,不检查响应是否为空,这就是我们得到错误的原因
if((response && jQuery.isArray(response))?(response.length==0?false:(jQuery.isPlainObject(response[0])?!jQuery.isEmptyObject(response[0]):response[0])):response){
//console.log("adfdsaf");
}
success: function(response) {
if((response && jQuery.isArray(response))?(response.length==0?false:(jQuery.isPlainObject(response[0])?!jQuery.isEmptyObject(response[0]):response[0])):response){
('#a1').append('No data');
}else{
date = response[0].Timestamp;
$('#a1').append(date);
}
};
},