jQuery+Ajax+js實現(xiàn)請求json格式數(shù)據(jù)并渲染到html頁面操作示例
更新時間:2020年06月02日 12:03:14 作者:wit_stan
這篇文章主要介紹了jQuery+Ajax+js實現(xiàn)請求json格式數(shù)據(jù)并渲染到html頁面操作,結合實例形式分析了jQuery+Ajax請求json格式數(shù)據(jù)并渲染到html頁面相關步驟與操作技巧,需要的朋友可以參考下
本文實例講述了jQuery+Ajax+js實現(xiàn)請求json格式數(shù)據(jù)并渲染到html頁面操作。分享給大家供大家參考,具體如下:
1、先給json格式的數(shù)據(jù):
[ {"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頁面,獲取并展示數(shù)據(jù):
方法一:
<!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數(shù)據(jù)轉換 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相關內容可查看本站專題:《jquery中Ajax用法總結》、《jQuery擴展技巧總結》、《jQuery常用插件及用法總結》、《jQuery常見經典特效匯總》及《jquery選擇器用法總結》
希望本文所述對大家jQuery程序設計有所幫助。
相關文章
html5的自定義data-*屬性和jquery的data()方法的使用示例
人們總喜歡往HTML標簽上添加自定義屬性來存儲和操作數(shù)據(jù),這就是為什么在HTML5規(guī)范里增加了一個自定義data屬性,你可以拿它做很多有用的事情2013-08-08基于jquery trigger函數(shù)無法觸發(fā)a標簽的兩種解決方法
下面小編就為大家分享一篇基于jquery trigger函數(shù)無法觸發(fā)a標簽的兩種解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01