2015-09-27 64 views
1

我使用jQuery Ajax和JSON從MySQL獲取數據。不過,我需要知道響應何時爲空。我通過一個日期參數到我的PHP,將返回一些數據或不... ...取決於日期。jQuery Ajax Json響應 - 檢查是否爲空

我的Javascript代碼(簡版):

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); 
      }, 
     }); 
    } 

舉例來說,如果我用昨天(2015年9月26日)的數據,我得到以下JSON數據(僅其中的一部分):

現在
[{"Timestamp":"2015-09-26 16:50:00","pac":"35.20","irra":"38.97"},{"Timestamp":"2015-09-26 17:00:00","pac":"32.19","irra":"35.51"}] 

,例如,如果我選擇了一個日期而沒有數據則返回:

[] 

在我的javascript代碼如下I W烏爾德喜歡一個if語句在JSON數組的情況下,添加到我的成功函數是空的......這樣的:

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'); 
        }; 
       }, 
      }); 
     } 

在我的成功函數我有一個JSON數據創建一個圖表莫里斯...我不不把它放在代碼中...

那麼如何檢查它是否爲空?我已經做了很多的嘗試:

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); 
} 
?> 
+0

可以使用response.length == 0 –

+1

https://stackoverflow.com/questions/16350604/javascript-how-to-test-if-的可能的複製response-json-array-is-empty – runz0rd

+0

'var rep = JSON.parse(response);如果(rep!= null || rep ==「」)' – aldrin27

回答

1

嘗試以下,如果將檢查各種條件

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); 

       } 
       }; 
      }, 

希望它會工作

+0

它看起來像你的代碼有錯誤... –

+0

對不起,複製條件時做了一個錯誤,希望它現在能工作... –

+0

我試過你的代碼...但沒有任何反應 \t \t \t \t if((response && jQuery.isArray(response))?(response.length == 0?false:(jQuery.isPlainObject(response [0])?!jQuery。isEmptyObject(響應[0]):響應[0])):響應){ \t \t \t \t \t如果(響應== [] ||響應== [{}] ||響應[ 「」]){\t \t \t \t \t \t \t $('#chart_title')。append('No data!'); \t \t \t \t \t}; –