xml与ajax联合实例

来源:互联网 发布:mac默认管理员密码 编辑:程序博客网 时间:2024/06/02 06:10

php文件

<?php
    header('Content-type:text/xml;charset=utf-8');
$str='<root>
<title>赵四</title>
<title>刘能</title>
<title>赵玉田</title>
     </root>';
    echo $str;
?>

html文件

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="jquery.js"></script>
</head>
<body>
<input type="button" value="获取新闻列表" id="btn" />
<div id="box">
<ul></ul>
</div>
<script>
$('#btn').click(function(){
var ul=$('#box>ul');
$.ajax({
url:'wode.php',
type:'get',
dataType:'xml',
success:function(xml){
$(xml).find('title').each(function(){
ul.append('<li>'+$(this).text()+'</li>')
})
}
})
})
</script>
</body>
</html>

1 0