小编这次要用示例详解jQuery+Ajax+js如何实现请求json格式数据并渲染到html页面,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。
本文实例讲述了jQuery+Ajax+js实现请求json格式数据并渲染到html页面操作。分享给大家供大家参考,具体如下:
1、先给json格式的数据:
[
{"id":1,"name":"stan"},
{"id":2,"name":"jack"},
{"id":3,"name":"lucy"},
{"id":4,"name":"mary"},
{"id":5,"name":"jerry"},
{"id":6,"name":"tom"}
]
2、通过访问html页面,获取并展示数据:
方法一:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<body>
<div id="test">
</div>
<script type="text/javascript">
window.οnlοad=function(){
//js代码请求
}
$(function(){
$.ajax({
method:"post",
url:"http://localhost:81/getpersons",/*这里要写nginx访问的全路径*/
data:{},
dataType: "json",
success: function(data){
var str="<ul>";
$.each(data,function(i,items){
str+="<li>"+"ID:"+items.id+"</li>";
str+="<li>"+"姓名:"+items.name+"</li>";
});
str+="</ul>";
$("div").append(str);
}
});
})
</script>
</body>
</html>
方法二:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<body>
<div id="test">
<table border="1" cellspacing="1" cellpadding="1" id="a1">
</table>
</div>
<script type="text/javascript">
window.οnlοad=function(){
//js代码请求
}
$(function(){
$.ajax({
method:"post",
url:"http://localhost:81/getpersons",/*这里要写nginx访问的全路径*/
data:{},
success: function(data){
alert(data);
//将json数据转换
dd=eval("("+data+")");
var htmls;
for(var i=0;i<dd.length;i++){
htmls="<tr>+<td>"+"id: "+dd[i].id+"</td>+<td>"+"name :"+dd[i].name+"</td>+</tr>";
$("#a1").append(htmls);
}
}
});
})
</script>
</body>
</html>
看完这篇关于用示例详解jQuery+Ajax+js如何实现请求json格式数据并渲染到html页面的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。