码迷,mamicode.com
首页 > Web开发 > 详细

ajax循环输出数据通常用的2种方式

时间:2018-03-03 00:37:54      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:ajax   col   $.ajax   成功   循环   error   data   bsp   name   

 1 <script>  
 2             $(function(){  
 3                 $("#btn").click(function(){  
 4                     $.ajax({  
 5                         type:"get",  
 6                         dataType:"json",  
 7                         url:"data.json",  
 8                         data:{},  
 9                         success:function(res){  
10                             $(".alert").html("请求成功")                   
13                             var html=‘‘;  
16                             //第一次方式  
17                 //   $.each(res,function(i,v){  
18                 //     html+="<li data-id="+v.id+">"+v.name+"</li>"  
19                 //   })  

20 //第二次方式 21 for(var i=0;i<res.length;i++){ 22 html+="<li data-id="+res[i].id+">"+res[i].name+"</li>" 23 } 24 25 26 $(".list").html(html) 27 28 }, 29 error:function(){ 30 $(".alert").html("请求失败") 31 } 32 }) 33 }) 34 $("#clear").click(function(){ 35 $(".list").html("") 36 $(".alert").html("") 37 }) 38 39 }) 40 </script>

 

ajax循环输出数据通常用的2种方式

标签:ajax   col   $.ajax   成功   循环   error   data   bsp   name   

原文地址:https://www.cnblogs.com/huanyumingmei/p/8495539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!