1、jQuery读取JSON
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery读取JSON</title>
</head>
<script type="text/javascript" src="jquery-3.2.1.js"></script>
<script>
$(function(){
$("#btn").click(function(){
$.getJSON("JSON/UserInfo.json",function(data){
var str="";
$.each(data,function( i,field){
$.each( field,function(k,v){
str+=k+":"+v+"<br/>"
})
str+="<br/>";
})
$("p").html(str);
})
})
})
</script>
<body>
<div>
<div id="btn">
<input type="button" value="读取JSON数据" id="btn"/>
</div>
<p></p>
</div>
</body>
</html>
2、jQuery加载XML文档
<!DOCTYPE html>
<html>
<head>
<title>使用jQuery加载XML文档</title>
<meta charset="utf-8">
<script type="text/javascript" src="jquery-3.2.1.js"></script>
<script type="text/javascript">
$(function(){
$("#getxml").click(function(){
$.ajax({
type:"get",
url:"5-3.xml",
dataType:"xml",
timeout:1000,
cache:false,//禁止缓存
success:function(data){
var frag=$("<ul/>");
$(data).find("student").each(function(){
var name=$(this).children("name");
var sex=$(this).children("sex");
var email=$(this).children("email");
name_value=name.text();
sex_value=sex.text();
email_value=email.text();
frag.append("<li>姓名:"+name_value+"性别:"+sex_value+"邮箱:"+email_value+"</li>");
});
frag.appendTo("#load");
$("li").css("list-style-type","none");
}
});
})
})
</script>
</head>
<body>
<input type="button" name="" id="getxml" value="加载xml">
<div id="load"></div>
</body>
</html>