2011-06-09 46 views
0

我有以下代碼jquery ajax返回true但不顯示正確的消息?

$(document).ready(function(){ 
// class exists 
if($('.delete').length) { 
     // add click handler 
    $('.delete').click(function(){ 
     // ask for confirmation 
     var result = confirm('Are you sure you want to delete this?'); 
     var vid  = $(this).attr('id'); 
     var row = $(this).parents('tr'); 
     // do ajax request 
     if(result) { 
      $.ajax({ 
       type:"POST", 
       url:"delete_joke.php", 
       data:{id:vid,garbage:'FG9g543hfh'}, 
       dataType: "json", 
       success:function(response){ 

        // hide table row on success 
        if(response == "true") { 
         row.fadeOut(); 
         $('#errors').show(); 
         $('#errors').html('<div class="message green"> Successfully deleted!<img src="http://puppetweb.ca/play/views/admin/gfx/icon-close.gif" alt="Close this item" /> </div>').show(); 

        }else{ 
         $('#errors').show(); 
         $('#errors').html('<div class="message red"> Error deleting. <img src="http://puppetweb.ca/play/views/admin/gfx/icon-close.gif" alt="Close this item" /> </div>').show(); 

        } 
       } 
      }); 
     } 
    return false; 
    }); 
} 
}); 

和它的作品,直到我想要展現成功的一部分,我有PHP腳本打印成功「真」與「假」的錯誤,如果我做警報(響應)它顯示單詞真實。但它顯示錯誤成功刪除和錯誤刪除錯誤?

任何幫助?

+0

請發帖也php代碼。也許問題在那裏。 – 2011-06-09 03:00:51

回答

0

如果您確定它正在返回true/false,請嘗試替換if(response == "true")if(response)

要檢查數據響應的類型,請嘗試alert(typeof response)並查看它顯示的內容。

編輯:

既然你提到的數據類型爲JSON,確保你返回一個有效的響應,並設置內容類型響應JSON。

如果您json回報是{"status":true},那麼你應該在這裏使用if(response.status)

0

的問題是範圍。當成功函數運行時不等於任何東西。最簡單的解決方案之一是傳回ID,將其確定,用作選擇器並隱藏。

如果我這樣做,我會從json切換到scrip並返回要執行的JavaScript。這似乎比真或假更有用。