企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## wx.request(OBJECT) >[danger]wx.request发起的是 HTTPS 请求。 ### index.js ~~~ //获取应用实例 var app = getApp() Page({ data: { indicatorDots:true, autoplay:true, interval:5000, duration:1000, banner: [] }, onLoad: function () { var that = this //调用应用实例的方法获取全局数据 reda(this,'banner'); } }) function reda(_self,type){ wx.request({ url: 'http://127.0.0.1/miniApp/my_app.php?type='+type, //仅为示例,并非真实的接口地址 header: { 'content-type': 'application/json' }, success: function(res) { console.log(res.data); _self.setData({ "banner":res.data.results, }); } }) } ~~~ >访问的网络接口: 如果是有 `AppID` 进行开发的必须配置网络服务器,同时接口地址请求必须是 `HTTPS` 请求, 本地测试如示例可以使用 `http://127.0.0.1/demo/API` ### 接口规则: 使用PHP来传送数据,转换为json数据: ~~~ <?php if(!isset($_GET['id']) || empty($_GET['id'])) { echo jsonFormat(array('error_code'=>300,'info'=>'请求失败')); } if($_GET['id']=='1') { $detail = array( 'id'=>'1', 'title'=>"详情1", 'img'=>"http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg", 'content'=>"框架的视图层" ); } $detail = array('error_code'=>200,'results'=>$detail); echo json_encode($detail); ?> ~~~ ### 模版调用: 示例中将 `{{banner}}` 映射为 `item`,来分别使用接口传送的数据 ~~~ <view class="container"> <view class="page"> <view class="section section_gap" wx:for="{{banner}}" wx:for-item="item"> <navigator url="{{item.url}}" class="img"> <view class="section__ctn"> <image class="section_img" src="{{item.img_path}}"></image> </view> <view class="section__title">{{item.title}}</view> </navigator> </view> </view> </view> ~~~