2013-07-29 39 views
0

我正在嘗試使用AJAX對我的網頁進行更改。 xml_whoid.php似乎正常工作。 jQuery的作品,直到我添加該功能(即使它只是警報)。我懷疑這是一個語法錯誤,但我找不到問題。當我嘗試使用返回值時,爲什麼我的AJAX代碼失敗?

我也不知道如何訪問返回的值。我有一本jQuery書,但沒有說明如何在比較語句中使用AJAX返回值。我用Google搜索很多次,但例子有 功能(結果){ 「過程這裏的數據」或「result.data.length」 }

看來如此簡單的問題,但我已經出去撕裂我的頭髮這是因爲我不想問另一個(愚蠢的)問題,但可以通過我的備份日期來說明,我一直試圖讓這個AJAX代碼工作5天。

     xml_whoid.php  
    <?php            
    require_once("../../includes/initialize.php"); 
    require_once("../../includes/functions.php"); 
    require_once("../../includes/session.php"); 
    require_once("../../includes/database.php"); 
    if ($session->is_logged_in()) { 
     $idwho=$_POST["idwho"]; 
     $session->changeidwho($idwho); 
     $whos = Whostaff::find_one_by_who($idwho); 
     if ($whos) { 
      echo "1"; 
     } else { 
      echo "0"; 
     } 
    } 
?> 


<script type="text/javascript"> 
    function changewho(idwho,mydesc) { 
     $('#who').html(mydesc); 
     $('#whofoot').html(mydesc); 
     $('#whomaint').html(mydesc); 
     $("#visible1").show(); 
     $("#visible2").show(); 
     $("#visible3").show(); 
     $("#visible4").show(); 
     $("#visible5").show(); 
     $.post('xml_whoid.php', { idwho: idwho }); 
     $.ajax({ 
      url: "xml_whoid.php", 
      type: "POST", 
      async: false, 
      data: { 
       idwho: idwho 
      }, 
     function(result) {     // This code causes everything to fail. 
      alert('Fetched');    // 
      //if(result==0){    // I have searched and searched but 
      // $("#visible6").show();  // I cannot work out if this is how 
      //}       // to access the returned value. 
     }        
    }); 
} 
</script> 

我使用「async:false」,因爲這個頁面並不經常使用,只在內部使用。

回答

3

你錯過了關鍵success

$.ajax({ 
    url: "xml_whoid.php", 
    type: "POST", 
    async: false, 
    data: { 
    idwho: idwho 
    }, 
    success: function(result) { 
    alert('Fetched'); 
    }        
}); 
1

AJAX調用有一個名爲success和另一error一個回調函數。
通過函數成功,您可以檢索從函數返回的消息。
隨着函數錯誤,你可以處理你的PHP頁面或錯誤的錯誤。 在你的代碼中你錯過了插入回調函數的名字。
試試這個:

$.ajax({ 
      url: "xml_whoid.php", 
      type: "POST", 
      async: false, 
      data: { 
       idwho: idwho 
      }, 
      success: function(result) {     
       alert('Fetched');    
      }, 
      error: function(error) {     
       alert(error);          
      }        
    }); 
0

返回功能應該寫成

success: function(result) { 
    alert('Fetched'); 
} 
相關問題