How to manipulate dataType xml when using $.ajax?
I am having some difficulties with the XML dataype when using $.ajax
.
I created a PHP file (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',
dataTy开发者_运维问答pe:"**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>
Here is my description for this example:
- When i click the 'TEST' button, it will call the AJAX to show the result of the PHP code
- In the AJAX call I only want to show the text of each tag in 2
<div>
below (#show1
,#show2
)
The above example is using dataType HTML in $.ajax
and it work well for this type. But when I tried with dataType XML, it didn't show anything.
So, I hope you guys give me some ideas or some references about this problem.
If the whole code you've posted is the content of test.php, the response can't be valid xml.
The output will end with <result/>
, but start's with a <script/>
.
(XML needs to have one root-element).
I would suggest to use the output_buffer, if the request comes via ajax discard the contents of the buffer before echoing the xml.(Or just put the PHP-code at the top )
精彩评论