2010-06-13 45 views
4

好吧,所以我有一個PHP腳本從數據庫獲取結果,並獲得這些結果我正在使用jQuery腳本通過getJSON提取結果。它完美的工作,但現在我想做的事情,如果PHP腳本返回沒有結果(空)。如何處理來自getJSON的空回報

我想:

$.getJSON('path/to/script'), {parameter:parameter}, function(data){ 
if (data) { 
    alert('Result'); 
} else { 
    alert('Empty); 
} 
}); 

但它沒有好。我已經嘗試了不同的東西,如if(data.length)但仍然沒有。我注意到,如果沒有返回的數據,回調將永遠不會觸發。所以如果是這樣的話,我該如何處理一個空的回報?

+0

您可能會發現http://api.jquery.com/jQuery.get/更加有用,這樣你就可以處理更多選項,因爲空的結果不是有效的JSON,所以沒有什麼可以解析的,因此沒有任何東西可以讓你做出反應。 – 2010-06-13 18:35:08

+2

你說得對,我也意識到我可以返回一個JSON編碼的空響應,然後可以通過回調進行檢查。呃..我真的需要開始思考。 – Gee 2010-06-13 18:40:02

+1

您應該將其作爲您的問題的答案發布,其他人可能會遇到同一問題。 – 2010-06-13 18:48:45

回答

7

你應該檢查你的PHP輸出總是輸出一些數據。

在我的代碼我添加了一個成功的標誌,我的輸出,這樣的:

$result = array (
'success' => true, 
'data' => $data 
); 
echo json_encode($result); 

如果我不輸出任何東西,我把假的變成成功的,這使得它易於通過數據jQuery的驗證.success:

例子:

$.getJSON('path/to/script', {parameter:parameter}, function(data){ 
if (data.success) { 
    alert('Result'); 
} else { 
    alert('Empty'); 
} 
}); 

如果你不想修改你的輸出,你可以設置一個ajaxError趕上你的閱讀問題。

例子:

$.ajaxError(function() { 
alert('error triggered'); 
}); 

PS:我不知道,如果缺少「在這行的末尾:

alert('Empty); 

在你原來真的丟失,如果是這樣,你錯過了」 ;-) >>

alert('Empty'); 
+0

我喜歡這種方法。 – 2011-07-06 14:00:12

+0

第二個代碼塊似乎錯過了(某處,最後一個似乎沒有匹配 對不起,我不是很熟悉lang,所以所有類型的匹配括號非常有幫助 – bonitzenator 2016-02-03 11:30:00

+0

@bonitzenator你是對的,我刪除了一個'''! TY! – favo 2016-02-19 16:19:01

0

根據documentation,如果你沒有收到任何回覆,.getJSON會給你null。但是,如果您傳遞的是諸如{}的內容,則最終會出現object,該內容不具有.length等屬性,並且在if條件下將被視爲true

如果你需要找出一個空的對象,有可能是一個更短的方式,但發生了我此刻的一個是:

var isEmpty = true; 
for (var p in data) { isEmpty = false; break; } 
// at this point, if isEmpty should tell you if 'data' is an empty object 

這個可憐的代碼工作假設data是對象類型。

+1

if(data.hasOwnProperty(p)){isEmpty = false; if(data_hasOwnProperty(p));打破; } }' – 2010-06-13 20:02:43

2
$.getJSON('json.php?',function(r){ 

       try { 
        typeof(r[0].city); 
       } catch (e) { return false; /* or do somthing */ } 
[...] 
+2

這也可以防止服務中斷(根本不會返回任何內容,甚至不會返回空的JSON對象)。 – 2012-03-25 11:18:12

0

這裏是浩w我在JS中處理空的響應從PHP獲取數據!

response.php

if (check parameters from script.php) { 
    $result = $con->query(sqlAll($con)); // 'sqlAll()' returns a query like 'SELECT * FROM table' 
    $arrData = array(); $i = 0; $tot = array(); $empty = ''; // '$empty' we'll use for empty response 

    if ($result->num_rows > 0) { 
     while ($row = $result->fetch_assoc()) { 

      // fill array 
      $arrData = array (
       'title' => $row["post_title"], 
       'author' => $row["post_author"] 
      ); 

      $tot[$i] = $arrData; // collect data from '$arrData' 
      $i++; 
     } 

     // tell PHP to become 'JSON-MASTER' 
     header('Content-type: application/json'); 

     echo json_encode($tot); // transfer response to script.php 
    } 
    else echo json_encode($empty); 

的script.php

$('#search_txt').keyup(function(){ // instant search 
    $.getJSON('response.php', { 
     // some parameters to be sent 
     }, 

     function(data){ 
      var row = ""; 
      $.each(data, function(key, val) { 
      row += 
       '<h2>'+ 
       '<a href="#">'+val.title+ 
       '</a></h2>'+ 
       '<p class="lead">'+ 
       'by <a href="index.php">'+val.author+'</a>'+ 
       '</p>'; 
      }); 

     $('.box').html(row); // display result to the page 
     } 
    ) 
}); 
+0

感謝您的糾正。 – 2015-12-03 09:22:20