1 <script type="text/javascript">
2 $(function(){
3 //点击按钮异步加载数据,并在content中显示
4 $("#btn").click(function(){
5
6 /**** $.ajax()方法 ***
7 // $.ajax({
8 // url: "data.json",//"发送的请求地址",
9 // type:"get",//"请求方式"
10 // data:null,//"要发送的数据",
11 // dataType: "json",
12 // beforeSend: function(data) { // 发送请求前执行的代码
13 // $(".loading").show();
14 // },
15 // success: function(data) {// 发送成功后执行的代码
16 // dealData(data);
17 // },
18 // complete:function(){
19 // $(".loading").hide();
20 // },
21 // error: function(XMLHttpRequest, textStatus, errorThrown) {// 请求失败执行的代码
22 // alert("错误信息:"+textStatus);
23 // }
24 // });
25 //
26
27 /**** $.getJSON()方法 ****/
28 // $.getJSON("data.json",function(data){
29 // dealData(data);
30 // })
31
32 /**** $.get()方法 ****/
33 // $.get("data.json",{},function(data){
34 // dealData(data);
35 // },"json");
36
37 /**** $.post()方法 ****/
38 $.post("data.json",{},function(data){
39 dealData(data);
40 },"json")
41
42
43 });
44 function dealData(data){
45 if(typeof data != "undefined"){
46 $(data.news).each(function(i){
47 $("#newsList").append("<li><a href='#id="+data.news[i].id+"'>"+data.news[i].title+"["+data.news[i].date+"]</a></li>")
48 });
49 }
50 }
51 });
52 </script>