這久自己在自己摸索apicloud,一直都是打算使用自己的數據庫來開發,因此這里我通過一個實例來獲取自己服務器數據庫中的欄目信息,
PHP代碼如下:
//JSON獲取欄目
public function Category(){
$Category = M('news_cate')->order('sort')->select();
$this->ajaxReturn($Category);
}
APICLOUD html如下:
<div class="find_nav">
<div class="find_nav_left">
<div class="find_nav_list" id="index_mun">
<ul>
<!--這里是我要獲取到inde_x_mun的欄目列表-->
</ul>
</div>
</div>
<a class="search_logo" href="">搜索</a>
</div>
<script type="text/javascript">
$(function(){
apiready = function () { //注意,使用apicloud的 api時必需要使用apiready
api.showProgress();//顯示加載進度框
api.ajax({
url: 'http://ww.zhix.net/Index/Index/Category', //例如:這是我的地址
method: 'post',
dataType: 'json',
returnAll:false,
data:{
values:{cid:1}
},
}, function(ret, err) {
api.hideProgress();//隱藏加載進度框
if(ret){
html = "";
for(var i=0;i<ret.length;i++){
if(i == 0){
var cl = '<li class="find_nav_cur">';
}else{
var cl = '<li>';
}
html += cl+'<a onclick="open_mun('+ret[i].id+')">'+ret[i].name+'</a></li>';
}
html +='<li class="sideline"></li>';
$('#index_mun ul').append(html);//在ul元素結尾插入html內容
}else{
api.alert({msg:('錯誤碼:'+err.code+';錯誤信息:'+err.msg+'網絡狀態碼:'+err.statusCode)});
}
});
}
});
</script>
這樣我們真機調試的時候就能看到欄目了。是不是很方便呢
