小編這次要用示例詳解jQuery+Ajax+js如何實(shí)現(xiàn)請(qǐng)求json格式數(shù)據(jù)并渲染到html頁面,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
我們提供的服務(wù)有:成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、成都外貿(mào)網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、宜君ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的宜君網(wǎng)站制作公司
本文實(shí)例講述了jQuery+Ajax+js實(shí)現(xiàn)請(qǐng)求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代碼請(qǐng)求 } $(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代碼請(qǐng)求 } $(function(){ $.ajax({ method:"post", url:"http://localhost:81/getpersons",/*這里要寫nginx訪問的全路徑*/ data:{}, success: function(data){ alert(data); //將json數(shù)據(jù)轉(zhuǎn)換 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>
看完這篇關(guān)于用示例詳解jQuery+Ajax+js如何實(shí)現(xiàn)請(qǐng)求json格式數(shù)據(jù)并渲染到html頁面的文章,如果覺得文章內(nèi)容寫得不錯(cuò)的話,可以把它分享出去給更多人看到。
分享名稱:用示例詳解jQuery+Ajax+js如何實(shí)現(xiàn)請(qǐng)求json格式數(shù)據(jù)并渲染到html頁面
分享地址:http://chinadenli.net/article14/ppihde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、商城網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)建站、建站公司、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)