2011-05-31 21 views
0

使用$.ajax時,我遇到了一些XML數據類型的問題。

我創建一個PHP文件(test.php):

<script type="text/javascript" src="js/jquery.min.js"></script> 

<?php   
    //this code will show some xml tag when there is an ajax call 
    if(isset($_REQUEST['t']) && $_REQUEST['t']==1){ 
     echo "<result >"; 
     echo "<info>Tristan Jun</info>"; 
     echo "<age>22</age>"; 
     echo "</result>"; 
     return; 
    } 
?> 

<script> 
$(document).ready(function(){ 
    $('#testing').click(function(){ 
     alert('uuuuuuu'); 
     var content = $.ajax({ 
      type:"GET", 
      url :"test.php", 
        data:'t=1', 
      dataType:"**html**", 
      async:false, 
      success:function(content){   
       cont = $(content); 
       inf  = cont.find('info').text(); 
       age = cont.find('agel').text(); 
      //alert('inf'); 
       $('#show1').html(inf); 
       $('#show2').html(total); 
      }, 
      error: function(){ 
       alert('THERE'S AN ERROR'); 
      } 
     }).**responseHTML**; 
     });//end of click 
});//end of ready 
</script> 

<a id="testing" href="#">TEST</a> 

<div id="show1"></div> 
<div id="show2" style="background-color:#069"></div> 

這裏是我的這個例子說明:

  • 當我點擊 '測試' 按鈕,它會調用AJAX展示PHP代碼
  • 的結果在AJAX調用中,我只想顯示下面的2個<div>中的每個標記的文本(#show1,#show2

上面的示例在$.ajax中使用了dataType HTML,它適用於此類型。但是,當我嘗試使用dataType XML時,它沒有顯示任何內容。

所以,我希望你們給我一些關於這個問題的想法或參考。

回答

0

如果您發佈的整個代碼是test.php的內容,則該響應不能是有效的xml。

輸出將以<result/>結束,但以<script/>開頭。 (XML需要有一個根元素)。

我會建議使用output_buffer,如果請求出自通過AJAX呼應XML之前丟棄緩衝區的內容。(或者只是把頂部的PHP代碼)

+0

耶!!,非常感謝。這是完美的工作:D。我忘記了xml ^^的結構 – Tristan 2011-05-31 03:28:16

相關問題