小型網站不使用vue框架如何實現資料渲染到前端頁面
阿新 • • 發佈:2018-11-09
1,先寫ajax請求
eg:
<script src="jquery.min.js"></script> <script> $(function(){ var xhrul='http://192.168.31.133:3000/jdapi'; $.ajax({ type:'get', async:false, url:xhrul, cache:false, dataType:"jsonp", success:function(json){ console.log(json); }, err:function(e){ alert("error") } }) }) </script>
2,在success裡寫資料繫結(只是示例,不全)
success:function(json){ console.log(json); var html=''; //定義變數 $("#climit li").html(""); //選中標籤 $(json.categorys).each(function(i,categorys){ //json.categorys的categorys是資料咯的最外面那個陣列,each迴圈遍歷 html+='<li class="relative">......</li>(html內容)'; html+='<p>......</p>'; $(categorys.categoryItems).each(function(i,categoryItems){ html+=''; }) }) },
採用物件的形式取值