[英]How to manipulate dataType xml when using $.ajax?
使用$.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>
這是我對這個例子的描述:
<div>
中顯示每個標簽的文本( #show1
, #show2
) 上面的例子是在$.ajax
中使用 dataType HTML ,它適用於這種類型。 但是當我嘗試使用 dataType XML 時,它沒有顯示任何內容。
所以,我希望你們給我一些想法或一些關於這個問題的參考。
如果您發布的整個代碼是 test.php 的內容,則響應不能是有效的 xml。
output 將以<result/>
結尾,但以<script/>
開頭。 (XML 需要有一個根元素)。
我建議使用 output_buffer,如果請求來自 ajax,則在回顯 xml 之前丟棄緩沖區的內容。(或者只是將 PHP 代碼放在頂部)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.